1

In the below example, the \spvec macro generates a column vector nicely in the main body of the document, but it won't compile in the caption.

\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}

\def\spvec#1{\left(\vcenter{\halign{\hfil$##$\hfil\cr \spvecA#1;;}}\right)}
\def\spvecA#1;{\if;#1;\else #1\cr \expandafter \spvecA \fi}

\begin{document}

A $test=\spvec{1;2}$ vector.

    \begin{figure}
        \centering
        \textbf{IMAGE HERE}
        \caption{abc... $\spvec{1;2}$}
    \end{figure}

\end{document}

The error is

Illegal parameter number in definition of \reserved@a.
<to be read again> 
                   $
l.17        \caption{abc... $\spvec{1;2}$}
spraff
  • 1,501
  • Related: http://tex.stackexchange.com/questions/186933/latex-how-to-write-a-mathematics-symbol-in-caption/186934#186934 – Ignasi Mar 10 '17 at 11:15
  • 1
    BTW: You should use low level commands like \def only, if you cannot use high level commands like \newcommand, \renewcommand or \providecommand. In the example even using \DeclareRobustCommand*{\spvec}[1] instead of \def\spvec#1 could be a good idea. – Schweinebacke Mar 10 '17 at 11:18

1 Answers1

1

You have two possibilities:

  • If you don't have a list of figures, or if you don't want to have the vector show up in this list, use

    \caption[abc... entry for list of figures without \spvec]{abc... $\spvec{1;2}$}
    

    or leave the entry completely empty

    \caption[]{abc... $\spvec{1;2}$}
    
  • "Protect" the command \spvec against being broken when moved around.

    \caption{abc... $\protect\spvec{1;2}$}
    

For an explanation see What is the difference between Fragile and Robust commands?.

gernot
  • 49,614