1

Is there any way to escape the scope of an environment, e.g. to get the \pgfplotsset command in the MWE below have an effect on the subsequent plot?

(I know that \newcommand doesn't have this problem but that isn't the question.)

Thanks!

\documentclass{article}
\usepackage{pgfplots}

\newenvironment{foo}{}{\pgfplotsset{axis x line=bottom};} % how can I make this pgfplotsset count?

\begin{document}
\begin{tikzpicture}

\begin{foo}
\end{foo}

\begin{axis}
\addplot[domain=-3e-3:3e-3,samples=201]{exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}

\end{document}
JPi
  • 13,595

1 Answers1

4

One possibility is to use \aftergroup, as suggested by Christian in his comment.

Note that this approach only allows you to "escape" the scope of the foo environment, i.e. one level up; if the foo environment is not at the global scope, the pgfplots settings will not be applied globally.

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\newenvironment{foo}{}{}
\newcommand\fixedpgfplotsset{\pgfplotsset{axis x line=bottom}}
\let\oldendfoo\endfoo
\def\endfoo{\oldendfoo\aftergroup\fixedpgfplotsset}

\begin{document}
\begin{tikzpicture}

%\bgroup %(as an experiment, uncomment this group)
\begin{foo}
\end{foo}
%\egroup    

\begin{axis}
\addplot[domain=-3e-3:3e-3,samples=201]{exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
\end{axis}
\end{tikzpicture}

\end{document}
jub0bs
  • 58,916
  • Thanks again @Jubobs. The problem I'm having is that the contents of \pgfplotsset are generated inside the foo environment, so it won't work as stated. I'll accept in anyway since it gave me an adequate understanding of the issues involved. Playing around with your example produced a different question; see http://tex.stackexchange.com/questions/195080/using-latex-commands-inside-pgfplotsset – JPi Aug 06 '14 at 09:27
  • 1
    Incorporating something like \expandafter\pgfplotsset\expandafter{\contents} did the trick; thanks all for your help! – JPi Aug 06 '14 at 20:40