# Doing math inside of pgfplotsset

I am declaring an environment to help with drawing binomial distributions. One of the natural parameters to give to such a function is "n", the number of trials. My plotting function, however, needs to plot 0 .. n-1 samples. I am finding that I am unable to do the "-1" within a pgfplotsset. Can anyone help me do this? MWE:

\documentclass{minimal}

\usepackage{pgfplots}

\newenvironment{BinomDist}[1]{%
\pgfplotsset{DistAxis/.style={%
samples at={0,...,#1} % Want this to say #1-1'' or equiv.
}
}
}{}

\begin{document}

\begin{BinomDist}{8}
\begin{tikzpicture}
\begin{axis}[DistAxis]
\end{axis}
\end{tikzpicture}
\end{BinomDist}

\end{document}


This is very simple: samples at={0,...,\the\numexpr#1-1}.

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.16} %<-consider using this
\newenvironment{BinomDist}[1]{%
\pgfplotsset{DistAxis/.style={%
samples at={0,...,\the\numexpr#1-1}
}
}
}{}

\begin{document}

\begin{BinomDist}{8}
\begin{tikzpicture}
\begin{axis}[DistAxis]
\end{axis}
\end{tikzpicture}
\end{BinomDist}

\end{document}


BTW, please consider not to use the minimal class, and to indicate the version.

For more complicated cases you might want to use code with \pgfmathtruncatemacro.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} %<-consider using this
\newenvironment{BinomDist}[1]{%
\pgfplotsset{DistAxis/.code={%
\pgfmathtruncatemacro{\iMax}{pow(2,#1-1)}%
\pgfplotsset{samples at={0,...,\iMax}}%
}
}
}{}

\begin{document}

\begin{BinomDist}{8}
\begin{tikzpicture}
\begin{axis}[DistAxis]
\end{axis}
\end{tikzpicture}
\end{BinomDist}
\end{document}

• Why is using the minimal environment a poor choice? I thought it existed for situations like this where I want to create an example with the simplest possible environment so that the situation is focused on the issue. I was following the guidance here: tex.stackexchange.com/questions/20974/… where the answer suggests using minimal for MWE's. – Christopher Donham Jan 26 at 3:49
• @ChristopherDonham My worries can be found under this thread. While I do not think that for the current example these concerns are too important, I feel that the reasons mentioned there are enough motivation to try to avoid propagating this class more than necessary. – Schrödinger's cat Jan 26 at 3:52