
編輯:這篇文章最初被命名為“Why is siunitx throwing an \endcsname inside newcommand?”,但我認為重命名確實更好地反映了問題的意圖。
為什麼這段程式碼(追蹤到 siunitx\unit
指令)會拋出一個\endcsname
? 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}
謝謝
答案1
我不覺得這一切那麼麻煩。您是否被連結答案的其餘部分迷惑了?
\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}