4

Consider the following MWE:

\documentclass{article}
\usepackage{calc} %\widthof

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{adjustbox}

\usepackage{tikz}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
% \usepackage{etoolbox}
\usepackage{xstring}
\usepackage{siunitx}

\begin{document}

\def\firstRowA{0}
\def\lastRowA{5}

\def\firstRowB{45}
\def\lastRowB{80}

\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
  group style={
    group name=my fancy plots,
    group size=2 by 1,
    yticklabels at=edge left,
    %xticklabels at=edge bottom,
    %vertical sep=0pt,
    horizontal sep=0pt,
  },
  %width=8.5cm,
  height=6cm,
  ymin=-6, ymax=6,
  domain=0:80,
]

\pgfmathsetmacro{\xminA}{\firstRowA+0.5} %
\pgfmathsetmacro{\xmaxA}{\lastRowA+0.5} %
\typeout{xminA \xminA, xmaxA \xmaxA}

\nextgroupplot[
  xmin=\xminA,xmax=\xmaxA,
  xtick={0,5,10},
  axis y line=left,
  %axis x discontinuity=parallel, % disc. is at start, so avoid for first
  axis x line=bottom,
  x axis line style=-, % switch off the axis arrow tips,
  %width=4.5cm, % don't set width,
  x=0.1cm,      % set x scale (for width)
]
\addplot {x*0};
\addplot {x*0+1};


\pgfmathsetmacro{\xminB}{\firstRowB+0.5} %
\pgfmathsetmacro{\xmaxB}{\lastRowB+0.5} %
\typeout{xminB \xminB, xmaxB \xmaxB}

\nextgroupplot[
%   xmin=45.5,xmax=80.5, % this works fine
  xmin=\xminB,xmax=\xmaxB,
  axis y line=none,
  xtick={60,80},
  axis x discontinuity=parallel,
  axis x line=bottom,
  %width=2.0cm, % don't set width,
  x=0.1cm,      % set x scale (for width)
]
\addplot {0*x};
\addplot {2+0*x};

\end{groupplot}

\end{tikzpicture}
\end{center}

\end{document}

If you compile it as-is, it will fail with:

! Undefined control sequence.
\pgfplots@loc@TMPa ->\xminB 

l.73 ]

... which is weird, because the \typeout just before that:

xminA 0.5, xmaxA 5.5
xminB 45.5, xmaxB 80.5

... confirms that, indeed, the \xminB macro is defined and does have a value?

If numbers are entered directly (that is, xmin=45.5,xmax=80.5,) - then the MWE compiles fine, and results with the expected rendering:

test02.png

Why does this happen - and how can I use a macro for xmin in this position?


EDIT: just solved the problem - seems that I need to "globalize" the second pair of macros: just add this in the code above:

...
\pgfmathsetmacro{\xminB}{\firstRowB+0.5} %
\pgfmathsetmacro{\xmaxB}{\lastRowB+0.5} %
\typeout{xminB \xminB, xmaxB \xmaxB}

% globalize - with expand!
\xdef\xminB{\xminB} % add redefinition
\xdef\xmaxB{\xmaxB} % add redefinition

\nextgroupplot[
...

... then the MWE compiles fine. But I'm still puzzled - how come I do not have to "globalize" the macro pair for the first \nextgroupplot; and yet I must "globalize" the second one?

2 Answers 2

4

You're doing the setting of \xmaxB and \xminB in a group.

\pgfmathsetmacro{\temp}{\firstRowB+0.5}\global\let\xminB\temp
\pgfmathsetmacro{\temp}{\lastRowB+0.5}\global\let\xmaxB\temp

will make the definition of the macros transcend the group boundary.

1
  • Thanks for that, @egreg - wasn't aware that there was a group boundary here; cheers!
    – sdaau
    Jun 5, 2014 at 13:01
1

A quick fix is to move the definitions of \xminB/\xmaxB to just after the definitions of \xminA/\xmaxA, though I cannot say exactly why. I assume \nextgroupplot creates a group, so that the definition becomes local to the first groupplot.

\documentclass{article}
\usepackage{calc} %\widthof

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{adjustbox}

\usepackage{tikz}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathreplacing}
% \usepackage{etoolbox}
\usepackage{xstring}
\usepackage{siunitx}

\begin{document}

\def\firstRowA{0}
\def\lastRowA{5}

\def\firstRowB{45}
\def\lastRowB{80}

\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
  group style={
    group name=my fancy plots,
    group size=2 by 1,
    yticklabels at=edge left,
    %xticklabels at=edge bottom,
    %vertical sep=0pt,
    horizontal sep=0pt,
  },
  %width=8.5cm,
  height=6cm,
  ymin=-6, ymax=6,
  domain=0:80,
]

\pgfmathsetmacro{\xminA}{\firstRowA+0.5} %
\pgfmathsetmacro{\xmaxA}{\lastRowA+0.5} %
\typeout{xminA \xminA, xmaxA \xmaxA}

\pgfmathsetmacro{\xminB}{\firstRowB+0.5} %
\pgfmathsetmacro{\xmaxB}{\lastRowB+0.5} %
\typeout{xminB \xminB, xmaxB \xmaxB}

\nextgroupplot[
  xmin=\xminA,xmax=\xmaxA,
  xtick={0,5,10},
  axis y line=left,
  %axis x discontinuity=parallel, % disc. is at start, so avoid for first
  axis x line=bottom,
  x axis line style=-, % switch off the axis arrow tips,
  %width=4.5cm, % don't set width,
  x=0.1cm,      % set x scale (for width)
]
\addplot {x*0};
\addplot {x*0+1};




\nextgroupplot[
%   xmin=45.5,xmax=80.5, % this works fine
  xmin=\xminB,xmax=\xmaxB,
  axis y line=none,
  xtick={60,80},
  axis x discontinuity=parallel,
  axis x line=bottom,
  %width=2.0cm, % don't set width,
  x=0.1cm,      % set x scale (for width)
]
\addplot {0*x};
\addplot {2+0*x};

\end{groupplot}

\end{tikzpicture}
\end{center}

\end{document}
2
  • Many thanks for that @TorbjørnT. - I have just realized that I can also use a global redefinition to fix that problem (see OP edit), so the question is now a bit more towards why does this happen. Cheers!
    – sdaau
    Jun 5, 2014 at 13:00
  • @sdaau As I say (guess), if \nextgroupplot starts a new group, then the \xminA will be defined before and outside the first groupplot. As it is defined outside the group, it is available inside it. But \xminB is defined inside the group, so it is not available outside. Jun 5, 2014 at 13:21

You must log in to answer this question.

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