我有这个图 (/ 2 个图)
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}
\begin{document}
\begin{figure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={Y}]
\addplot[black,samples=100,domain=0:100] {x};
\end{axis}
\end{tikzpicture}
}
\caption{a}
\label{fig:a}
\end{subfigure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{
\begin{tikzpicture}
\begin{axis}[
scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={Y}]
\addplot[black,samples=100,domain=0:100] {sqrt(x)};
\end{axis}
\end{tikzpicture}
}
\caption{b}
\label{fig:b}
\end{subfigure}
\caption{cap}
\end{figure}
\end{document}
由于其中有一些,但只有 y 标签和函数发生了变化,我认为可以编写一个命令来“添加”其余部分,即不发生变化的内容,但我不知道该怎么做。这是我尝试过的:
\newenvironment{splot}[1]{
\begin{tikzpicture}
\begin{axis}[scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={#1}]}
{\end{axis}\end{tikzpicture}}
\newcommand{plotfun}[1]{\addplot[
black,samples=100,domain=0:100]{#1};}
\begin{document}
\begin{splot}[$Y label$]
\plotfun{sqrt(x)}
\end{splot}
\end{document}
但显然\addplot[black,samples=100,domain=0:100]{#1};
不能将其放入这样的命令中。
另外,是否可以将两个图形并排显示。它们始终位于一列而不是一行,尽管我已经尝试将它们缩小……?
更新:所以我接受了 David 的提示,跳过了定义 new 的环节addplot
,这可能更难。以下代码现在可以编译,但图形无法并排显示。为什么会这样?
\begin{figure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{
\begin{splot}{$Y label$}
\addplot[black,samples=100,domain=0:100]{x};
\end{splot}
}
\caption{a}
\label{fig:a}
\end{subfigure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{
\begin{splot}{$Y label$}
\addplot[black,samples=100,domain=0:100]{sqrt(x)};
\end{splot}
}
\caption{b}
\label{fig:b}
\end{subfigure}
\caption{cap}
\end{figure}
答案1
使用样式(推荐方法):
您应该使用
tikzset
来定义自己的样式。下面,我My Axis Style
为您的轴样式和My Plot Style
您的图定义了样式:笔记:
- 删除两个子图之间的空白行将使两个图彼此相邻。这类似于普通文本段落中的行为,其中空白行表示新段落的开始。
自定义环境:
您尝试定义的自定义环境的主要问题是
\newcommand{plotfun}{...}
必须如此
\newcommand{\plotfun}{...}
下面的替代解决方案向您展示了如何拥有自定义环境并提供可选参数
splot
和\plotfun
以允许进一步自定义。为了便于说明,我使用可选参数来splot
更改第一个图的轴样式,并使用可选参数来\plotfun
更改线样式:
代码:使用样式:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}
\tikzset{/pgfplots/My Axis Style/.style={
scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={Y},
}
}
\tikzset{My Plot Style/.style={black,samples=100,domain=0:100, ultra thick}}
\begin{document}
\begin{figure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{% <--- Note
\begin{tikzpicture}
\begin{axis}[My Axis Style]
\addplot[My Plot Style, blue] {x};
\end{axis}
\end{tikzpicture}%
}
\caption{a}
\label{fig:a}
\end{subfigure}% <--- Note
% <--- Note
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{% <--- Note
\begin{tikzpicture}
\begin{axis}[My Axis Style]
\addplot[My Plot Style, red] {sqrt(x)};
\end{axis}
\end{tikzpicture}% <--- Note
}
\caption{b}
\label{fig:b}
\end{subfigure}
\caption{cap}
\end{figure}
\end{document}
代码:自定义环境
\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\newcommand{\xfigscale}{.5}
\newcommand{\yaxscale}{.75}
\tikzset{/pgfplots/My Axis Style/.style={
scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={Y},
}
}
\tikzset{My Plot Style/.style={black,samples=100,domain=0:100, ultra thick}}
\newenvironment{splot}[2][]{
\begin{tikzpicture}
\begin{axis}[scale only axis,
height=\yaxscale\textwidth,
axis lines=middle,
axis line style={->},
x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
y label style={at={(axis description cs:-0.1,.5)},rotate=90,anchor=south},
xlabel={X},
ylabel={#2},
#1
]
}{%
\end{axis}\end{tikzpicture}%
}
%% Note The command name is "\plotfun" not "plotfun"
\newcommand{\plotfun}[2][]{\addplot[My Plot Style,#1]{#2};}
\begin{document}
\begin{figure}
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{%
\begin{splot}[axis line style={<->, magenta},]{$Y_1$}
\plotfun[blue]{x}
\end{splot}%
}
\caption{$y=x$}
\label{fig:a}
\end{subfigure}% <--- Note
% <--- Note
\begin{subfigure}[b]{\xfigscale\textwidth}
\centering
\resizebox{\linewidth}{!}{%
\begin{splot}{$Y_2$}
\plotfun[red]{sqrt(x)}
\end{splot}%
}
\caption{$y=\sqrt{x}$}
\label{fig:b}
\end{subfigure}
\caption{Twp plots via custom environment.}
\end{figure}
\end{document}