边注

边注

\SetKwData我使用命令定义了一个宏algorithm2e,但它似乎在标题中不起作用。

我的代码:

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\SetKwData{Result}{result}

\begin{document}
\begin{algorithm}
  \KwIn{ \(x\) and \(y\)  }
  \KwOut{ \Result }
  \( \Result \leftarrow x + y \) \;
  \Return{ \Result  }
  \caption{ This is a caption with \Result. }
\end{algorithm}
In here I talk about the \Result of \(x + y \).

\end{document}

错误:

! Argument of \@caption has an extra }.
<inserted text> 
                \par 
l.72   \caption{ This is a caption with \Result. }

答案1

您需要或使用\protect可选\result的简短标题。

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\SetKwData{Result}{result}

\begin{document}
\begin{algorithm}
  \KwIn{ \(x\) and \(y\)  }
  \KwOut{ \Result }
  \( \Result \leftarrow x + y \) \;
  \Return{ \Result  }
  \caption{ This is a caption with \protect\Result. }
\end{algorithm}
In here I talk about the \Result of \(x + y \).

\end{document}

边注

除了缺少\protect,您的输入还有几个错误的空格。回想一下,在数学模式下空格会被忽略,但 TeX 排版文本时并非总是如此。因此您应该更加小心:\Return{ \Result }与 不同\Return{\Result}

\documentclass[10pt,a4paper,twoside]{report}

\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\SetKwData{Result}{result}

\begin{document}
\begin{algorithm}
  \KwIn{\(x\) and \(y\)}
  \KwOut{\Result}
  \( \Result \leftarrow x + y \) \;
  \Return{\Result}
  \caption{This is a caption with \protect\Result.}
\end{algorithm}
In here I talk about the \Result of \(x + y \).

\end{document}

相关内容