\renewcommand
と\RenewDocumentCommand
で挙動が変わるものがあり、理由がよくわからず気になっています。
再現できると思われる最小のソースを以下に示します。所望の出力は添付PDFのものです。
\renewcommand
を使った際† は\ref
を用いて「Listing 1.1に示す」「Listing 2.1に示す」と望み通り参照できるものの、\RenewDocumentCommand
を使った際†† は\ref
すると「Listing 1.0に示す」「Listing 2.0に示す」と番号がおかしくなってしまいます。
† \renewcommand{\thelstlisting}{\arabic{section}.\arabic{lstlisting}}
とした際
†† \RenewDocumentCommand\thelstlisting{}{\arabic{section}.\arabic{lstlisting}}
とした際
ひとまず\RenewDocumentCommand
ではなく\renewcommand
を使うことで対処していますが、この挙動の違いの理由が気になります。
理由がお分かりの方いらっしゃいましたら、ぜひご教示お願いします。
※ \AtBeginDocument
内の記述はhttps://ta-b0.hateblo.jp/entry/2020/08/13/001223
を参考にしました。
\documentclass{jlreq}
\usepackage{listings}
\makeatletter
\AtBeginDocument{
\renewcommand{\thelstlisting}{\arabic{section}.\arabic{lstlisting}} % うまくいく
% \renewcommand{\thelstlisting}{\arabic{section}.\protect\arabic{lstlisting}} % うまくいかない
% \RenewDocumentCommand\thelstlisting{}{\arabic{section}.\arabic{lstlisting}} % うまくいかない
\@addtoreset{lstlisting}{section}
}
\makeatother
\begin{document}
\section{hoge}
Listing~\ref{list:hoge}に示す。
\begin{lstlisting}[caption=hoge,label=list:hoge]
print("Hello, LaTeX!")
\end{lstlisting}
\section{fuga}
Listing~\ref{list:fuga}に示す。
\begin{lstlisting}[caption=fuga,label=list:fuga]
print("Oh, TeX...")
\end{lstlisting}
\end{document}
(LuaLaTeX)