1

I am using pgfplots to render the figures in my manuscript. The figures are kept in separate .tikz-files. Each figure contains data from multiple samples, 1 plot per sample. The measurements of multiple samples is obviously so as to demonstrate the reproducibility of the experiment. As the data of those samples agree well with each other, for clarity I would rather only show the plot of 1 sample. However, the full figure should go to the Appendix.

To do so, I needed a way to select the plots shown from a single .tikz-file, so I do not need to duplicate the file and I only need to make future adjustments to the one file.

main tex file

\documentclass{article}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{graphicx}
\begin{document}

    \section{results}   
    %% a switch so only one sample is shown 
    \begin{figure}[h]
        \input{greatfigure.tikz}
    \end{figure}

    \section{Appendices}
    %% a switch so all samples is shown 
    \begin{figure}[h]
        \input{greatfigure.tikz}
    \end{figure}

\end{document}

great figure.tikz

\begin{tikzpicture}
\begin{axis}[
    width=8.5cm,
    height=8.5cm,
    xmin=0,xmax=10,ymin=0,ymax=1,
    enlargelimits=false,
    clip = false,
    xlabel={X},
    ylabel={Y},
    ]
% some switch
    \addplot [only marks,mark=*,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
    table[row sep=crcr]{% this is sample 1
        0.5 0.12\\
        1.5 0.23\\
        3   0.35\\
        5   0.54\\
        7   0.74\\
        9   0.93\\
    };
% some switch
    \addplot [only marks,mark=square,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
    table[row sep=crcr]{% this is sample 2
        0.5 0.10\\
        1.5 0.21\\
        3   0.34\\
        5   0.54\\
        7   0.73\\
        9   0.91\\
    };
\end{axis}
\end{tikzpicture}
1

Solution 1

One solution uses a command as a switch. If the command matches a certain word, the second plot is shown.

main tex file

\documentclass{article}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{graphicx}

\newcommand{\showsamples}[1]{#1} % this will be the switch
\def\showsamples{} % for the case I forget to define it, which would give an error

\begin{document}

    \section{results}   
    \begin{figure}[h]
        \input{greatfigure1.tikz}
    \end{figure}

    \section{Appendices}
    \def\showsamples{YES}
    \begin{figure}[h]
        \input{greatfigure1.tikz}
    \end{figure}

\end{document}

greatfigure1.tikz

\begin{tikzpicture}
\begin{axis}[
    width=8.5cm,
    height=5cm,
    xmin=0,xmax=10,ymin=0,ymax=1,
    enlargelimits=false,
    clip = false,
    xlabel={X},
    ylabel={Y},
    ]
        \addplot [only marks,mark=*,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
        table[row sep=crcr]{% this is sample 1
            0.5 0.12\\
            1.5 0.23\\
            3   0.35\\
            5   0.55\\
            7   0.74\\
            9   0.93\\
        };
    \ifcase\pdfstrcmp{\showsamples}{YES}
        \addplot [only marks,color=red,mark=square,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
        table[row sep=crcr]{% this is sample 2
            0.5 0.10\\
            1.5 0.21\\
            3   0.3\\
            5   0.52\\
            7   0.73\\
            9   0.91\\
        };
    \fi
\end{axis}
\end{tikzpicture}

Solution 1


Solution 2

Alternatively, several plots can be selected by a slightly different approach.

main tex file

\documentclass{article}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{graphicx}

\newcommand{\showsamples}[1]{#1}
\def\showsamples{} % for the case I forget to define it, which would give an error

\begin{document}

    \section{results}

    \def\showsamples{1}
    \begin{figure}[h]
        \input{greatfigure2.tikz}
    \end{figure}

    \section{Appendices}

    \def\showsamples{3}
    \begin{figure}[h]
        \input{greatfigure2.tikz}
    \end{figure}

\end{document}

greatfigure2.tikz

\begin{tikzpicture}
\begin{axis}[
    width=8.5cm,
    height=5cm,
    xmin=0,xmax=10,ymin=0,ymax=1,
    enlargelimits=false,
    clip = false,
    xlabel={X},
    ylabel={Y},
    ]
    \ifnum\ifnum\showsamples=1 1\else\ifnum\showsamples=3 1\else0\fi\fi
    =1 % 
        \addplot [only marks,mark=*,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
        table[row sep=crcr]{% this is sample 1
            0.5 0.12\\
            1.5 0.23\\
            3   0.35\\
            5   0.55\\
            7   0.74\\
            9   0.93\\
        };
    \fi
    \ifnum\ifnum\showsamples=2 1\else\ifnum\showsamples=3 1\else0\fi\fi
    =1 %
        \addplot [only marks,color=red,mark=square,mark size=2pt,line width=1pt,error bars/.cd,x dir=both,x fixed=0.3,error bar style={very thick}]
        table[row sep=crcr]{% this is sample 2
            0.5 0.10\\
            1.5 0.21\\
            3   0.3\\
            5   0.52\\
            7   0.73\\
            9   0.91\\
        };
    \fi
\end{axis}
\end{tikzpicture}

Solution 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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