# Settting PGFplots axis environment options by means of \newcommand and {\newlength,\setlength}

Because I often use the same options for multiple axes in my LaTeX documents, I use the following code to simplify my life.

\documentclass{article}
\usepackage{pgfplots}
\newcommand{\linecolora}{blue}
\newcommand{\marka}{o}
\newcommand{\plotoptsa}{color=\linecolora,mark=\marka}
\newcommand{\xlabela}{Test test 123}
\newlength\figurewidth
\setlength{\figurewidth}{\columnwidth}
\newcommand{\axisoptsa}{width=100,xlabel=\xlabela}
\newcommand{\axisoptsb}{ymin=-8,ymax=-3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[\axisoptsa,\axisoptsb]
{
(2,-2.8559703)
(3,-3.5301677)
(4,-4.3050655)
(5,-5.1413136)
(6,-6.0322865)
(7,-6.9675052)
(8,-7.9377747)
};
\end{axis}
\end{tikzpicture}
\end{document}


It works like a charm! However, if I change width=100 into the defined width=\figurewidth, I get Missing \endcsname inserted. error. Using

\expandafter\begin{axis}\expandafter[\axisoptsa,\axisoptsb]


does not help.

You shouldn't do stuff like that with newcommands, rather define your own styles. This makes it possible to override them. Defining styles ensures, that the diagrams you are producing all have the same look and style.

Instead of having everything as flexible as you have it, I would suggest one style per type of diagram.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\newlength\figurewidth
\setlength{\figurewidth}{\columnwidth}
\newcommand{\marka}{o}
%You can define colors like this:
\colorlet{linecolora}{blue}
%and define new styles like this:
\pgfplotsset{
xlabela/.style={Test test 123},
axisA/.style={width=\figurewidth,xlabel={xlabela}},
axisB/.style={ymin=-8,ymax=-3},
plotoptsa/.style={color=linecolora,mark=\marka}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[axisA,axisB,xlabel={manually and conscious override of style}]
coordinates
{
(2,-2.8559703)
(3,-3.5301677)
(4,-4.3050655)
(5,-5.1413136)
(6,-6.0322865)
(7,-6.9675052)
(8,-7.9377747)
};
\end{axis}
\end{tikzpicture}
\end{document}

• Works great! Thanks a lot. I have a few comments though :). 1) The \newcommand{\plotoptsa}{color=\linecolora,mark=\marka} can now be removed. 2) The point was to have the width variable, so I'd define axisA/.style={xlabel={\xlabela},width=\figurewidth}. So remove width=100 and add ,width=\figurewidth}. – Adriaan Nov 18 '13 at 15:03
• And how do you make the legend dependent? A \newcommand{#1}{#2} does not work because the comma's are misinterpreted. And \legend{#1} does not except styles. – Adriaan Nov 18 '13 at 16:17
• I don't know what you mean. Could you be a bit more specific? How is \newcommand{#1}{#2} supposed to work? – Johannes_B Nov 18 '13 at 19:07
• I want to use something like \legend{\legendA} in an axis environment with, say, two graphs. I specify \legendA by means of \newcommand{\legendA}{graph A,graph B}. But then I get just one legend entry graph A,graph B instead of the two legend entries graph A and graph B. So the comma is not interpreted properly and I do not know how to solve this. I found several other questions here at tex.stackexchange.com on for instance comma-separated-list but I can't seem to solve my problem. – Adriaan Nov 18 '13 at 22:50