I want to construct a new command which allows me to write somthing like:


Which would then expand to:


I.e. a sigma with the specified number of primes. I however have not been able to correctly construct a command or definition which accomplishes this.

  • do you need support for an arbitrary number as the argument or can there be a maximum supported?
    – Bordaigorl
    Sep 18, 2014 at 9:42
  • I would prefer an arbitrary number, but probably I will not need more than three to four primes.
    – jsfr
    Sep 18, 2014 at 9:44
  • I provided a very simple minded solution that puts a superscript with the number for argument>4. It seems a more complicated solution is not worth it. The complication is that LaTeX needs to see all the primes at once or either you get a double superscript error or the wrong spacing
    – Bordaigorl
    Sep 18, 2014 at 9:48

3 Answers 3


A classical implementation allowing also exponents:



We call repeatedly \m@keprimes adding a prime to its first argument at each iteration. When we end the token list produced by \romannumeral#1000@ (a string of m's followed by @), we deliver the sequence of primes and the standard LaTeX trick for primes is applied.

However, I'm not sure \env{3} is clearer or easier to type than \sigma'''.

enter image description here

A better solution, also allowing exponents:

  \sigma\prg_replicate:nn{ #1 } { ' }


enter image description here

  • Both work like a charm :). Personally I prefer the second one though as it seems clearer, and avoids primitive TeX
    – jsfr
    Sep 18, 2014 at 10:01
  • the first solution is mesmerising. Still, deeply inside I do not want to know how it works =P
    – Bordaigorl
    Sep 18, 2014 at 10:36
  • @Bordaigorl The new version should be clearer. ;-)
    – egreg
    Sep 18, 2014 at 10:54
  • @egreg yes, a bit. Is the main trick generating the primes from the last one?
    – Bordaigorl
    Sep 18, 2014 at 10:57
  • ah no I get it it's accumulating them before letting TeX see them, right? Thanks for the explanation on \romannumeral that was bamboozling
    – Bordaigorl
    Sep 18, 2014 at 11:00

A simple solution supporting a max of four primes




  \env{2} $\sigma''$


I know it's ugly to type all the cases but in this way you can preserve the right spacing between primes, which won't be the case if you issue one ' at a time, unless more advanced tricks are employed.

  • With a loop you can support up to 16358 primes (granted you don't use e-TeX). :-)
    – 1010011010
    Sep 18, 2014 at 9:47
  • @1010011010 how do you avoid having a double superscript error?
    – Bordaigorl
    Sep 18, 2014 at 9:49

A classical implementation allowing also exponents:

\def\env#1{\sigma \tmpnum=#1\relax \envA{}}
\def\envA{\ifnum\tmpnum>0 \advance\tmpnum by-1 \expandafter\envB \else \expandafter\envC \fi}


You must log in to answer this question.

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