TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a PNG plot with a couple of black patches in it, and I'd like to refer to it in the \caption of of the figure. According to the TikZ manual, I'd have to

\tikz \fill[black] (1ex,1ex) circle (1ex);

which which works perfectly outside a \caption. Using this inside a \caption, though, leaves me with the compile error

! Use of \use@pgflibrary doesn't match its definition.
\pgfutil@ifnextchar ...1\def \pgfutil@reserved@a {
                                              #2}\def \pgfutil@reserved@...
l.375 ...blah blah.}


The following produces the compile error


\tikz \fill[orange] (1ex,1ex) circle (1ex);
\caption{\tikz \fill[orange] (1ex,1ex) circle (1ex);}

Any hints here?

share|improve this question
up vote 31 down vote accepted

The argument of \caption is a moving argument, i.e. it can also be displayed in the list of figures. It is therefore written into an external file. This does not work with fragile macros. See e.g. What is the difference between Fragile and Robust commands? for more details.

You can fix this by one of the following ways:

  1. Use the optional argument of \caption:

    \caption["Short" caption without tikz code]{Long caption with tikz code}

    In this case only the short version is placed in the external file and the list of figures.

  2. Add \protect for every macro:

    \caption{Some caption with tikz code: \protect\tikz \protect\fill[black] (1ex,1ex) circle (1ex);}
  3. Define a robust macro for the TikZ code:

    % At best in the preamble:
    \DeclareRobustCommand\mytikzdot{\tikz \fill[black] (1ex,1ex) circle (1ex);}
    % later
    \caption{Some caption with tikz code: \mytikzdot}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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