我正在使用 pgfplots 来呈现手稿中的图形。这些图形保存在单独的 .tikz 文件中。每个图形包含来自多个样本的数据,每个样本 1 个图。多个样本的测量显然是为了证明实验的可重复性。由于这些样本的数据彼此吻合得很好,为了清楚起见,我宁愿只显示 1 个样本的图。但是,完整的图应该放在附录中。
为此,我需要一种方法来从单个 .tikz 文件中选择显示的图,这样我就不需要复制文件,而只需要对一个文件进行将来的调整。
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
解决方案 1
一个解决方案是使用命令作为开关。如果命令与某个单词匹配,则显示第二个图。
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}
解决方案 2
或者,可以通过稍微不同的方法来选择几个图。
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}