4

edit: This post originally was named "Why is siunitx throwing an \endcsname inside newcommand?", but the rename does reflect the intention of the question better I think.

Why is this code (tracked it down to the siunitx \unit command) throwing an \endcsname? What are the general pitfalls with newcommand?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat=1.18}

\begin{document}
    \newcommand{\axissettings}{ymin=-1, xlabel={Test with $\unit{\celsius}$}}
    
    \begin{figure}
        \begin{tikzpicture}
            \begin{axis}[grid, \axissettings]
                \addplot coordinates {(0,0)};
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

Thank you

4
  • 2
    Fundamentally it is not an issue with \newcommand per se. The issue is more how you are using the new macro you created. Evidently you run into some expansion related issue by using it the way you do. For setting a style to be used with multiple pgfplots axes, you should not define the options in a \newcommand. It is better to define a custom style and load that style. See e.g. tex.stackexchange.com/a/475355/119 Apr 18 at 13:24
  • Thank you for the quick reply. uhmm, just wanted to insert some quick commands to not being forced to retype it for every graph I have. So there's no way to make it work? The solution you suggested is quite cumbersome and will be forgotten by me very soon.
    – VanC
    Apr 18 at 13:38
  • If you are working with pgfplots, pgf or tikz, you will need to utilise pgfkeys often enough to remember. And it isn't just pgfkeys. A lot of things now use key-value interfaces, so the general approach is becoming quite pervasive. LaTeX itself now provides key-value parsing etc. I assume it is unfamiliarity rather than being especially cumbersome: \tikzset{axissettings/.style={}} isn't significantly more cumbersome than \newcommand\axissettings{}, is it?
    – cfr
    Apr 18 at 16:54
  • 1
    That said, you can probably make something like you have work if you go to enough trouble. But it will be a lot more effort to fight pgfplots than to go with the flow as you'll have to manage the expansion.
    – cfr
    Apr 18 at 16:57

1 Answer 1

6

I don't find this all that cumbersome. Were you thrown off by the rest of the linked answer?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat=1.18}
\pgfplotsset{yourcustomaxissettings/.style={ymin=-1, xlabel={Test with $\unit{\celsius}$}}}

\begin{document}
    
    \begin{figure}
        \begin{tikzpicture}
            \begin{axis}[grid,yourcustomaxissettings,ymax=1,xmin=-1,xmax=1]
                \addplot coordinates {(0,0)};
            \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}
1
  • 1
    Indeed, I was thrown off by the rest of the linked answer. This works, thank you very much. I edited the name of the key to make it clear this is a custom attribute (I thought axissettings was a fixed name).
    – VanC
    Apr 22 at 13:08

You must log in to answer this question.

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