2

I want to add PGF plotmarkers to the caption of a Tikz figure. I defined commands to plot these markers like

\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}

That works well within a text (although I have a spacing problem after the marker), but within a caption it won't compile. Do you have any suggestions?

My MWE is:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{smithchart}

\usetikzlibrary{
    arrows,
    positioning,
    calc,
    circuits.ee.IEC,
    external,
    plotmarks}

\usetikzlibrary{pgfplots.units}    

\pgfplotsset{ every axis label/.append style={font=\scriptsize}, every tick label/.append style={font=\scriptsize,text depth=.25ex}}
%set labels in all diagrams in scriptsize

\pgfkeys{/pgf/number format/.cd, use comma, set thousands separator={ }}
% use comma for dezimal numbers; no use of thoudends seperator

\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}

\begin{document}

\begin{figure}[t]    % Example plot with markers
    \centering
    \begin{tikzpicture}
    \begin{smithchart}[scale=1]
    \pgfsetplotmarksize{0.7ex}
    \addplot[draw=blue,mark=o, only marks, is smithchart cs] coordinates { 
    (0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534) 
    };
    \addplot[draw=red,mark=x, only marks, is smithchart cs] coordinates { 
    (0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
    };
    \end{smithchart}
    \end{tikzpicture}
    \caption{Example caption. I want to add the marker here.}
\end{figure}

Example text with PGFmarker like \showpgfcircle included. This works, but there is no space after the marker.

\end{document}
Daniel
  • 35
  • 4
  • You've gotten an answer, but see https://tex.stackexchange.com/questions/56079/using-tikz-inside-a-figure-caption and https://tex.stackexchange.com/questions/31091/space-after-latex-commands – Torbjørn T. Feb 06 '18 at 11:08
  • Also, you can add a \label{blueplot} after the \addplot command, and get the marker with \ref{blueplot}. Albeit maybe in a smaller size. – sodd Feb 06 '18 at 11:14
  • I tried it first with \label and \ref , but this didn't work and I don't know why, so I tried to fix it with my own commands. – Daniel Feb 06 '18 at 11:17
  • 1
    @Daniel See this question: tikz legend in caption :) – sodd Feb 06 '18 at 11:27
  • @hooy Yes, I found that thread before asking my question. I tried this solution, as it is basically really simple. But it produced errors while compiling. And I couldn't really figure out why. I'll probably try it with a MWE when I have time, but for now I'm satisfied with the working solution. :) – Daniel Feb 07 '18 at 12:11

1 Answers1

3

Your code was almost correct. Only two things missing.

First, the lack of space you mentioned is because TeX eats spaces after a macro name (see chapter 3 of the TeXBook, its very short and explains a lot). You have to enforce a space after the macro with \⍽ ( indicates a normal space).

Additional note: There is the xspace package, that performs various checks and tries to determine whether a space should or should not be inserted after the macro name, so you should not need to worry about typing \⍽ after the macro name. But before you use it, see the Drawbacks of xspace.

If you opt to use xspace, the your macro becomes:

\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};\xspace}

and you use it with:

The macro \showpgfcircle will insert a space in the first case, but not here: \showpgfcircle.

Second, to add a tikzpicture to the caption you have to \protect it because the figure caption is written to the .lof file to create the list of figures. See What is the purpose of \protect? and What is the difference between Fragile and Robust commands?.

Here is your code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{smithchart}

\usetikzlibrary{
    arrows,
    positioning,
    calc,
    circuits.ee.IEC,
    external,
    plotmarks}

\usetikzlibrary{pgfplots.units}    

\pgfplotsset{ every axis label/.append style={font=\scriptsize}, every tick label/.append style={font=\scriptsize,text depth=.25ex}}
%set labels in all diagrams in scriptsize

\pgfkeys{/pgf/number format/.cd, use comma, set thousands separator={ }}
% use comma for dezimal numbers; no use of thoudends seperator

\def\showpgfcircle{\tikz[baseline=-0.9ex]\node[blue,mark size=0.7ex]{\pgfuseplotmark{o}};}

\begin{document}

\begin{figure}[t]    % Example plot with markers
    \centering
    \begin{tikzpicture}
    \begin{smithchart}[scale=1]
    \pgfsetplotmarksize{0.7ex}
    \addplot[draw=blue,mark=o, only marks, is smithchart cs] coordinates { 
    (0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534) 
    };
    \addplot[draw=red,mark=x, only marks, is smithchart cs] coordinates { 
    (0.041457198, -0.56082138) (-0.46495696, -0.31631368) (-0.50641416, 0.2445077) (-0.041457198, 0.56082138) (0.46495696, 0.31631368) (0.50641416, -0.2445077) (-0.42592593, 0.73772534) (0.42592593, 0.73772534) (0.42592593, -0.73772534) (-0.42592593, -0.73772534)
    };
    \end{smithchart}
    \end{tikzpicture}
    \caption{Example caption. I want to add the marker here: \protect\showpgfcircle\ see? }
\end{figure}

Example text with PGFmarker like \showpgfcircle\ included. This works, but there is no space after the marker.

\end{document}

enter image description here