How do I use the variables of (nested) tikz foreach loops in the legend of a plot in math mode?
This MWE fails:
\documentclass[tikz]{standalone} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \foreach \a in {1,2}{ \foreach \b in {1,2}{ \addplot {\a*\b*x}; \addlegendentry{$a=\a$, $b=\b$}; % LaTeX Error: Command \$ unavailable in encoding OT1. } } \end{axis} \end{tikzpicture} \end{document} This is possibly related to foreach loop in tikz and math mode and How can I use \foreach in math environments?, but those pages don't seem to solve my problem.
