我正在尝试学习如何使用切换条件来应用于我正在创建的包含图表的文档。
以下是创建 MWE 的摘录:
\documentclass[border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{xstring}
\newcommand{\NomFigure}{Echelon}
\begin{document}
\begin{tikzpicture}
\def\Ezero{2}
\def\Arampe{2}
\newcommand{\doLimit}[1]{%
\IfStrEqCase{#1}{
{Echelon}{
\def\LimiAxeT{2}
\def\LimiAxeY{\Ezero*1.5}}
{Rampe}{
\def\LimiAxeT{2}
\def\LimiAxeY{2*\Arampe}
}
}[\def\LimiAxeT{2}
\def\LimiAxeY{2}]
}
\doLimit{\NomFigure}
\newcommand{\doAxis}[1]{%
\IfStrEqCase{#1}{
{Echelon}{
extra y ticks={\Ezero},
extra y tick labels={$A$},
}
{Rampe}{
extra y ticks={\Arampe},
extra y tick labels={$a$},
extra x ticks={1},
extra x tick labels={$1$},
}
}[nada]
}
\begin{axis}[
major grid style={dashed,black,line width=0.7pt},
axis lines=middle,
width=5cm, height=4cm,
ymax=\LimiAxeY, xmax = (\LimiAxeT+0.3),
ytick=\empty, ylabel={$e(t)$}, y label style={red},
ymin=-0.1, xmin=-0.6,
x label style={below}, xlabel={$t$},
xtick=\empty,
thick,
% \doAxis{\NomFigure}
% Echelon
extra y ticks={\Ezero},
extra y tick labels={$A$},
% Rampe
% extra y ticks={\Arampe},
% extra y tick labels={$a$},
% extra x ticks={1},
% extra x tick labels={$1$},
]
% Echelon
\draw[red] (axis cs:-0.4,0) -- (axis cs:0,0) -- (axis cs:0,\Ezero) -- (axis cs:3,\Ezero);
% Rampe
%\draw[red] (axis cs:-0.4,0) -- (axis cs:0,0) -- (axis cs:2,2*\Arampe) ;
%\draw[dashed] (axis cs:1,0) -- (axis cs:1,\Arampe) -- (axis cs:0,\Arampe) ;
\end{axis}
\end{tikzpicture}
\end{document}
如果你像这样运行它,它将产生一个像我想要的图像,这表明第一个开关条件(\newcommand{\doLimit}
)工作正常。
以下是我面临的两个问题
如果我输入
\NomFigure
一个 switch 无法识别的值(此处不是Rampe
或Echelon
),代码应该运行 之间的部分[...]
,如果我理解得没错的话。但是当我尝试时,我得到了一个编译错误File ended while scanning use of \@xs@testcase.
更重要的是,第二个开关(
\newcommand{\doAxis}
)不起作用。我试图根据要绘制的图来更改轴属性,但似乎\doAxis{\NomFigure}
在轴内调用不起作用。而且我得到了一个永无止境的编译,所以我甚至无法给出错误消息......
你能帮助我吗 ?
答案1
我会使用/.is choice
密钥。您可以教它忽略未知选项。我还会使用execute at begin axis
pgf 密钥而不是宏。
\documentclass[tikz,border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,
my do limit/.is choice,
my do limit/Echelon/.style={/pgfplots/limi axe/.cd,
T=2,Y=\pgfkeysvalueof{/pgfplots/limi axe/Ezero}*1.5},
my do limit/Rampe/.style={/pgfplots/limi axe/.cd,
T=2,Y=\pgfkeysvalueof{/pgfplots/limi axe/Arampe}*2},
do limit/.style={/errors/unknown choice value/.code={},my do limit/.try={#1}},
my do axis/.is choice,
my do axis/Echelon/.style={extra y ticks={\pgfkeysvalueof{/pgfplots/limi axe/Ezero}},
extra y tick labels={$A$},
execute at begin axis={
\draw[red] (axis cs:-0.4,0) -- (axis cs:0,0) --
(axis cs:0,\pgfkeysvalueof{/pgfplots/limi axe/Ezero})
-- (axis cs:3,\pgfkeysvalueof{/pgfplots/limi axe/Ezero});
}},
my do axis/Rampe/.style={extra y ticks={\pgfkeysvalueof{/pgfplots/limi axe/Arampe}},
extra y tick labels={$a$},
extra x ticks={1},
extra x tick labels={$1$},
execute at begin axis={
\draw[red] (axis cs:-0.4,0) -- (axis cs:0,0) -- (axis cs:2,2*\pgfkeysvalueof{/pgfplots/limi axe/Arampe}) ;
\draw[dashed] (axis cs:1,0) -- (axis cs:1,\pgfkeysvalueof{/pgfplots/limi axe/Arampe}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/limi axe/Arampe}) ;
}},
do axis/.style={/errors/unknown choice value/.code={},my do axis/.try={#1}},
limi axe/.cd,
T/.initial=2,
Y/.initial=3,
Arampe/.initial=2,
Ezero/.initial=2,
}
\begin{document}
\newcommand{\NomFigure}{Echelon}
\begin{tikzpicture}
\begin{axis}[
major grid style={dashed,black,line width=0.7pt},
axis lines=middle,
width=5cm, height=4cm,do limit=\NomFigure,
ymax=\pgfkeysvalueof{/pgfplots/limi axe/Y},
xmax ={(\pgfkeysvalueof{/pgfplots/limi axe/T}+0.3)},
ytick=\empty, ylabel={$e(t)$}, y label style={red},
ymin=-0.1, xmin=-0.6,
x label style={below}, xlabel={$t$},
xtick=\empty,
thick,
do axis=\NomFigure,
]
\end{axis}
\end{tikzpicture}
\renewcommand{\NomFigure}{Rampe}
\begin{tikzpicture}
\begin{axis}[
major grid style={dashed,black,line width=0.7pt},
axis lines=middle,
width=5cm, height=4cm,do limit=\NomFigure,
ymax=\pgfkeysvalueof{/pgfplots/limi axe/Y},
xmax ={(\pgfkeysvalueof{/pgfplots/limi axe/T}+0.3)},
ytick=\empty, ylabel={$e(t)$}, y label style={red},
ymin=-0.1, xmin=-0.6,
x label style={below}, xlabel={$t$},
xtick=\empty,
thick,
do axis=\NomFigure,
]
\end{axis}
\end{tikzpicture}
\renewcommand{\NomFigure}{Nonsense}
\begin{tikzpicture}
\begin{axis}[
major grid style={dashed,black,line width=0.7pt},
axis lines=middle,
width=5cm, height=4cm,do limit=\NomFigure,
ymax=\pgfkeysvalueof{/pgfplots/limi axe/Y},
xmax ={(\pgfkeysvalueof{/pgfplots/limi axe/T}+0.3)},
ytick=\empty, ylabel={$e(t)$}, y label style={red},
ymin=-0.1, xmin=-0.6,
x label style={below}, xlabel={$t$},
xtick=\empty,
thick,
do axis=\NomFigure,
]
\end{axis}
\end{tikzpicture}
\end{document}
答案2
对于那些因为尝试使用\IfStrEqCase
并且得到以下结果而发现此问题的其他用户:
File ended while scanning use of \@xs@testcase.
该命令似乎对空格非常敏感,尤其是在包含可选的默认情况时。这不起作用:
\IfStrEqCase{foo}{
{bar}{Bar}
}[other]
但这些将:
\IfStrEqCase{foo}{{bar}{Bar}}[other]
\IfStrEqCase{foo}{%
{bar}{Bar}%
}[other]