5

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

\env{3}

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.

3
  • 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

9

A classical implementation allowing also exponents:

\documentclass{article}

\makeatletter
\newcommand{\env}[1]{%
  \sigma
  \expandafter\startm@keprimes\expandafter{\romannumeral#1000@}%
}
\def\startm@keprimes#1{\m@keprimes{}#1}
\def\m@keprimes#1#2{%
  \if#2@%
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#1}%
  {\m@keprimes{#1'}}%
}
\makeatother
\begin{document}
$\env{1}\env{2}\env{3}^2$
\end{document}

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:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\env}{m}
 {
  \sigma\prg_replicate:nn{ #1 } { ' }
 }
\ExplSyntaxOff

\begin{document}
$\env{1}\env{2}\env{3}^2$
\end{document}

enter image description here

6
  • 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
5

A simple solution supporting a max of four primes

\documentclass{article}

\newcommand{\env}[1]{$
  \sigma\ifcase#1%
  \or'\or''\or'''\or''''\else^{#1}\fi
$}

\begin{document}

Example:
  \env{1}
  \env{2} $\sigma''$
  \env{4}
  \env{5}

\end{document}

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.

2
  • 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
4

A classical implementation allowing also exponents:

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

$\env{1}\env{2}\env{4}^2$             

You must log in to answer this question.

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