3

I want to create an macro to replace the string '{a,b,c}' in my code:

xticklabels={a,b,c}

so I set

\newcommand\inputnames{a,b,c}
xticklabels=\inputnames

but this gives me the same result as xticklabels={{a,b,c}}, which is different in my case. I suppose that it comes from an unwanted expansion of my macro but I can't find how to prevent it.

The complete code is bellow:

\documentclass{standalone}
\usepackage{pgfplots}


\begin{document}

\newcommand{\inputnames}{a,b,c}

\begin{tikzpicture}
\begin{axis}[xtick={1,2,3},xticklabels=\inputnames]
\addplot coordinates {(1,-1)(2,-3)(3,-4)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[xtick={1,2,3},xticklabels={a,b,c}]
\addplot coordinates {(1,-1)(2,-3)(3,-4)};
\end{axis}
\end{tikzpicture}

\end{document} 
6

You need to use

xticklabels/.expand once=\inputnames

which expands the argument to xticklabel once before actually applying it. (There’s also .expand twice and .expanded.)

Otherwise when the xticklabels are read, the parser only find one label, namely \xticklabel which is then given to the node that typesets this. Firstly here the macro is expanded.

Code

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\newcommand{\inputnames}{a,b,c}
\begin{tikzpicture}
\begin{axis}[xtick={1,2,3},xticklabels/.expand once=\inputnames]
\addplot coordinates {(1,-1)(2,-3)(3,-4)};
\end{axis}
\end{tikzpicture}
\end{document} 

Output

enter image description here

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.