13

I am wondering why I am getting errors when I try to add a caption to table using the \input command. A minimal example is as follows. The file cap2test.tex can be any text file. E.g., mine contains only the characters ``The first three''

\documentclass{article}
\begin{document}
\begin{table}
\caption{\input{cap2test.tex}}
\begin{tabular}{c}
asd
\end{tabular}
\end{table}
\end{document}

The error is probably going to be the same for other people:

./test.tex:4: Argument of \@caption has an extra }. \par l.4 \caption{\input cap2test.tex}

I'll also add that I have had such errors for years, and usually worked around it by not modularizing my tables or figures this way. Is it a bug?

Nicolae

3
  • 1
    This hasn't to do with the solution to your problem, but why do you use an \input inside a caption?
    – Aradnix
    Commented Oct 19, 2014 at 6:23
  • 2
    The table is produced automatically with a statistical package, would like to be able to update without retyping (pasting) the caption each time. (In fact, the table itself is inserted using \input, and the caption inserted in the table likewise.)
    – nicolae
    Commented Oct 19, 2014 at 19:21
  • 1
    To be clear, by having the caption in a different file, I can also input it in other tex files, such as presentation slides. That said, not clear that all this modular structure is that useful.
    – nicolae
    Commented Oct 19, 2014 at 19:25

2 Answers 2

13

A non-robust macro -- or "fragile" macro, to use LaTeX jargon -- in the argument of a \caption command will throw this error. Non-robust commands inside the argument of \caption must be \protected to avoid getting an error message. You should therefore write

\caption{\protect\input{cap2test.tex}}
5

You can use \DeclareRobustCommand to declare a command you can use in your caption.

\DeclareRobustCommand{\myname}{\input{cap.tex}}
\documentclass{article}
\begin{document}
\begin{table}
\caption{\myname}
\begin{tabular}{c}
asd
\end{tabular}
\end{table}
\end{document}
1
  • I tried \protect, which worked, but this is even better, b/c it makes the \caption command very short. I can add \label{} to \myname, etc.
    – nicolae
    Commented Oct 19, 2014 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .