在 KOMA scrbook 中加入 \chapter 會產生「!LaTeX 錯誤:\do undefined」。

在 KOMA scrbook 中加入 \chapter 會產生「!LaTeX 錯誤:\do undefined」。

情境:這個問題是後續問題處理巨集的逗號分隔參數清單中的 _(底線)。在這個問題中,我創建了一個宏\codecitep,它採用逗號分隔的列表作為參數,並列印這些生成hyperref連結的鍵。

問題:我的 MWE 工作得很好,但是,我沒有成功地在我的真實文件中實現它。我確實收到以下錯誤:

! LaTeX Error: \do undefined.

我已經找到了原因,那就是......命令\chapter\codecitep命令定位首先\chapter編譯得很好,但是那些不。

解決方案:(是的,解決方案先於問題!)正如建議的那樣找不到 LaTeX 錯誤\do在導致錯誤的命令之後重置定義使編譯工作。

然而,\def\do{}在每個\chapter命令後面添加是一種骯髒的方式,而且我不認為使用as 後綴重新定義/修補 KOMA 的\chapter命令是一個可持續的解決方案。\def\do{}

問題:\chapter改變的定義是什麼\do,以及如何避免它影響我自己的\codecitep宏觀?


\documentclass{scrbook}
    \usepackage{etoolbox}
    \usepackage{hyperref}
    \usepackage{lipsum}

    \newcommand{\codecitep}[1]{% cf. https://tex.stackexchange.com/a/87423/64454
        [%
        \def\nextitem{\def\nextitem{, }}% Separator
        \renewcommand*{\do}[1]{\nextitem{\hyperref[code:##1]{##1}}}% How to process each item
        \docsvlist{#1}% Process list
        ]%
    }   
\begin{document}

    \section{Body before chapter}
        A sentence with one code-citation only \codecitep{key1}.
        Another sentence with two code-citations and followed by dummy text \codecitep{key1, key2}.

\chapter{Chapter title}
%\def\do{}% <----- uncomment to make the error disappear
    \section{Body after chapter}
        A sentence with one code-citation only \codecitep{key1}.
        Another sentence with two code-citations and followed by dummy text \codecitep{key1, key2}.
        \lipsum[1-2]

    \section{Appendix}
        \lipsum[3]

        \subsection{key1}
        \label{code:key1}
        \label{code:a_123}
        \lipsum[4]

        \subsection{key2}
        \label{code:key2}
        \label{code:bb_456}
        \lipsum[5]
\end{document}

答案1

dostocbasic.sty使用的包scrbook

\let\do\relax

作為 的一部分\doforeachtocfile,由 執行\chapter。這意味著你不能這樣做\renewcommand\do{...},因為\do根據 LaTeX 是未定義的。

事實上,如果我\let\do\relax從兩個內部巨集中刪除它,問題就會消失。

\renewcommand{\do}{...}問題在其他情況下也曾出現過。在我看來,最好的方法是使用您自己的清單處理器。

\documentclass{scrbook}

\usepackage{etoolbox}
\usepackage{hyperref}
\usepackage{lipsum}

\newcommand{\codecitep}[1]{% cf. https://tex.stackexchange.com/a/87423/64454
  [%
  \def\nextitem{\def\nextitem{, }}% Separator
  \forcsvlist\codecitepitem{#1}% Process list
  ]%
}
\newcommand{\codecitepitem}[1]{%
  \nextitem
  \hyperref[code:#1]{\detokenize{#1}}%
}

\begin{document}

\section{Body before chapter}

A sentence with one code-citation only \codecitep{key1}.
Another sentence with two code-citations and followed by 
dummy text \codecitep{key1, key2}.

\chapter{Chapter title}

\section{Body after chapter}

A sentence with one code-citation only \codecitep{key1}.
Another sentence with two code-citations and followed by 
dummy text \codecitep{key1, key2}.
\lipsum[1-2]

\section{Appendix}

\lipsum[3]

\subsection{key1}

\label{code:key1}
\label{code:a_123}
\lipsum[4]

\subsection{key2}
\label{code:key2}
\label{code:bb_456}
\lipsum[5]

\end{document}

答案2

\do用於許多低階乳膠結構,只要瀏覽一下latex.ltx就有

 \global\let\do\noexpand

\begin{document}

  \let\do\@makeother 

\let\do\do@noligs

verbatim\verb

\let\do\@makeother

filecontents

\do我沒有準確地追蹤在您的用例中重置它的內容,但基本上它應該只在執行分隔清單之前立即用於本地定義。

相關內容