Graphics, Figures & TablesSmall drawn Picture within Caption

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
thomgraf
Posts: 9
Joined: Wed Jul 18, 2012 12:21 pm

Small drawn Picture within Caption

Post by thomgraf »

Has anyone an idea how to use a tikzpicture environment within a figure caption? What I want is, plot a blue circle withing a figure caption, like this.

Code: Select all

\begin{figure}[ht]
%...
\caption{CaptionText \begin{tikzpicture} \draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (0,0); \end{tikzpicture} CaptionText.}
\end{figure}
Within regular text, this works fine with this.

Code: Select all

RegularText \begin{tikzpicture} \draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (0,0); \end{tikzpicture} RegularText
Thank you!
Last edited by cgnieder on Tue May 21, 2013 6:24 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

thomgraf
Posts: 9
Joined: Wed Jul 18, 2012 12:21 pm

Small drawn Picture within Caption

Post by thomgraf »

The solution is this one.

Code: Select all

{\protect\tikz \protect\draw[thick, color=blue, fill=blue] plot[mark=*, mark options={scale=1.3}] (0,0);}
Last edited by cgnieder on Tue May 21, 2013 6:24 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Small drawn Picture within Caption

Post by cgnieder »

It seems you found the solution yourself. The problem is that the argument of \caption is what's called a moving argument. This means that its contents at some point will be completely expanded, usually when it is written to an auxiliary file as is the case for captions. Figure captions are written to the lof file which is used by the \listoffigures to display the list of figures. In a moving argument one cannot use fragile commands. fragile commands are commands that are not expandable and which are not taken care of by LaTeX's \protected@write. As you've experienced \tikz and \draw are not expandable which means they must be protected inside a moving argument such as \caption's which is usually done by placing \protect in front of them.

Regards
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Small drawn Picture within Caption

Post by localghost »

You seem to do some plots with TikZ/PGF. The pgfplots package not only makes plotting easier but also offers a nice feature that simply works with the known mechanism of \label and \ref. An example derived from Section 4.8.6 of the package manual.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{figure}[!htb]
    \centering
    \begin{tikzpicture}[baseline]
      \begin{axis}
        \addplot+[
          only marks,
          samples=15,
          error bars/y dir=both,error bars/y fixed=2.5
        ] {3*x+2.5*rand};
        \label{pgfplots:label1}
        \addplot+[mark=none] {3*x};
        \label{pgfplots:label2}
        \addplot {4*cos(deg(x))};
        \label{pgfplots:label3}
      \end{axis}
    \end{tikzpicture}
    \caption{The estimations \protect\ref{pgfplots:label1} which are subjected to noise. It appears the model \protect\ref{pgfplots:label2} fits the data appropriately. Finally, \protect\ref{pgfplots:label3} is only here to get three examples.}
    \label{fig:pgfplots-labels}
  \end{figure}
\end{document}
All \ref commands need to be prefixed by \protect in order to avoid problems with \listoffigures (see above code). The output is attached.


Thorsten
Attachments
ztmp.png
ztmp.png (21.64 KiB) Viewed 12091 times
Post Reply