0

I am trying to learn how to use switch conditions to apply in a document I am creating that contains plots.

Here is an extract to create a 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}

If you run it like this, it will produce an image like I want, which shows that the first switch condition (\newcommand{\doLimit}) is working fine.

Here are the two problems I'm facing

  • If i put inside \NomFigure a value that is not recognized by the switch (something other than Rampe or Echelon here), the code should run the part that is between [...], if I understood it. But when I tried I jest get a compilation error

    File ended while scanning use of \@xs@testcase.
    
  • More importantly, the second switch (\newcommand{\doAxis}) is not working. I am trying to change axis properties according to which plot I want to draw but it seems that calling \doAxis{\NomFigure} inside axis doesn't work. And I get a never ending compilation, so I cannot even give a error message...

Can you help me ?

1
  • Do not use \IfStrEqCase here. Use the pgf key /.is choice instead. And please explain better what you want to achieve.
    – user194703
    Nov 7, 2019 at 17:51

2 Answers 2

1

I would use the /.is choice key. You can teach it to ignore unknown options. I would also use execute at begin axis and use pgf keys instead of macros.

\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}

enter image description here

4
  • Thank you for your help, why is IfStrEqCase not the way to go ? I find two drawbacks to your solution: using \pgfplotsset{ before the begin document, meaning that I can't put my code in a single file to use in input. And the need for \pgfkeysvalueof{/pgfplots/limi axe/ each time you use a variable is quite heavy. Besides, I get an error if I do not remove compat=1.16
    – LMT-PhD
    Nov 7, 2019 at 22:45
  • @LMT-PhD 1. you can move the \pgfplotsset stuff after \begin{document}. 2. You can use /.store in=\macro in order to store a key in a macro. See the extended discussion under this post why I do not recommend it, others feel differently. 3. My code does compile fine on an updated TeXLive 2019 installation. And finally \IfStrEqCase will always be problematic w.r.t the expansion. I wouldn't want to mess with it.
    – user194703
    Nov 7, 2019 at 22:51
  • Well, and if you get an error if you do not remove compat=1.16 that most likely means that you did not update your TeX installation in a while.
    – user194703
    Nov 8, 2019 at 6:50
  • Thank you for your time, I'll try it out.
    – LMT-PhD
    Nov 8, 2019 at 8:25
0

For anyone else who finds this question because they're trying to use \IfStrEqCase and are getting:

File ended while scanning use of \@xs@testcase.

The command seems to be very sensitive to whitespace, especially when including the optional default case. This won't work:

\IfStrEqCase{foo}{
    {bar}{Bar}
}[other]

but these will:

\IfStrEqCase{foo}{{bar}{Bar}}[other]

\IfStrEqCase{foo}{%
    {bar}{Bar}%
}[other]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .