2

I use pgfplots with externalization. When I try to include a macro that uses \ifthenelse, always the 'else' statement is being executed. When I switch off externalization, it works again. See this example:

\documentclass{report}

\usepackage{pgfplots}
\usepackage{ifthen}

% my new command
\newcommand{\plotcmd}[1]{ 
    \begin{tikzpicture}
    % different title depending on argument:
    \begin{axis}[\ifthenelse{\boolean{#1}}{title={true}}{title={false}}]
    \addplot {x};
    \end{axis}
    \end{tikzpicture}}

% begin of externalization
\usetikzlibrary{external}
\usepgfplotslibrary{external}
\tikzset{external/force remake}
\tikzexternalize
% end of externalization

\begin{document}

% use either of those to switch \ifthenelse on and off 
\tikzexternalenable  % now \ifthenelse is off
%\tikzexternaldisable % now \ifthenelse is on

% test my new command
\plotcmd{true} % should be titled 'true'
\plotcmd{false} % should be titled 'false'

\end{document}

Whats going wrong here?

  • Seems to work for me: could we see the \listfiles output in your log? – Joseph Wright Dec 7 '11 at 19:21
  • you are correct, my example was wrong, ill renew it. – Hendrik Dec 7 '11 at 19:38
  • 1
    Your example doesn't compile for me, but fails with Argument of \boolean has an extra }.. I would have been surprised if this had worked, since you're trying to run a macro in your axis options. The example works fine if I use \ifthenelse{\boolean{#1}}{\pgfplotsset{title=true}}{\pgfplotsset{title=false}} before \begin{axis} in your command instead. – Jake Dec 8 '11 at 2:35
3

Jake's comment is the answer: the compilation failed. This can be verified by inspection of the <filename>-figure0.log .

The immediate cause for the failure is that \ifthenelse cannot be used in a place where keys are evaluated (because that command is not "expandable").

There are two ways to solve the problem:

  1. either by moving the \ifthenelse in front of the axis and inserting \pgfplotset into its body (as shown by jake)

or

  1. by moving the \ifthenelse into the argument for title. This is what I would suggest; it seems more local:

\newcommand{\plotcmd}[1]{ 
    \begin{tikzpicture}
    % different title depending on argument:
    \begin{axis}[title={\ifthenelse{\boolean{#1}}{true}{false}}]
    \addplot {x};
    \end{axis}
    \end{tikzpicture}
}

This does not explain why your experiment worked without externalization. From my point of view, it should never have worked ... in fact, on my computer, it does not work if I switch to \tikzexternaldisable.

| improve this answer | |

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.