4

I defined a macro with \SetKwData command from algorithm2e but it doesn't seem to be working in the caption.

My code:

\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}

The error:

! Argument of \@caption has an extra }.
<inserted text> 
                \par 
l.72   \caption{ This is a caption with \Result. }
  • A tip: if you select the code snippet and click on the {} button (or hit Control-K), the formatting will be better; the <pre>...</pre> method is to be avoided. – egreg Oct 7 '15 at 23:11
4

You need to \protect the \result or use the optional short caption.

\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}

Side note

Apart from the missing \protect, your input has several wrong spaces. Recall that spaces are ignored in math mode, but not in all cases when TeX is typesetting text. So you should be more careful: \Return{ \Result } is not the same as \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}
  • I was answering the same, but adding also something about the excess spaces in the input. – egreg Oct 7 '15 at 23:18
  • No, you came first. ;-) – egreg Oct 7 '15 at 23:19
  • Thanks for the side note. I think I got some errors in the past I solved with some additional spacing and just started spreading spaces all around. I'll try to read something about this. – Fernando César Oct 8 '15 at 8:22
  • @FernandoCésar: Sidenotes credit should go to the master. :) – user11232 Oct 8 '15 at 8:47
  • Thanks for the side note @egreg. I think I got some errors in the past I solved with some additional spacing and just started spreading spaces all around. I'll try to read something about this. – Fernando César Oct 8 '15 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.