2

I'm trying to get my counter (which has a value between 1--4) to display as a character (namely, the corresponding char from the string SWOT). The following looks right to me but does not do the trick:

\usepackage{xstring}

and then

\renewcommand{\theswot}{\StrMid{SWOT}{\value{swot}}{\value{swot}}}

That is, it's fine until I want to use

\refstepcounter{swot}

at which point this starts throwing all sorts of unintelligible errors.

I've tried many variations on the use of \StrMid, including sequences of \ifthenelse{\value{swot}=1}{S}{} and so on, but this does not seem to make any difference.

Any help is appreciated: why the errors, and how do I get what I want?

Added in response to @siracusa's comment:

Here's an MWE:

\documentclass{article}
\usepackage{ifthen}
\usepackage{xstring}
\begin{document}
\newcounter{swot}
\renewcommand{\theswot}{%
  \StrMid{SWOT}{\arabic{swot}}{\arabic{swot}}%
  % \ifthenelse{\arabic{swot}=1}{S}{}% This also doesn't work
  % \ifthenelse{\arabic{swot}=2}{W}{}%
  % \ifthenelse{\arabic{swot}=3}{O}{}%
  % \ifthenelse{\arabic{swot}=4}{T}{}%
}
\stepcounter{swot}\theswot % Prints S
\stepcounter{swot}\theswot % Prints W
\stepcounter{swot}\theswot % Prints O
\refstepcounter{swot}      % Throws errors
\end{document}
  • 1
    Please post a small example document that can be used to reproduce the errors. – siracusa Aug 29 '19 at 22:58
  • 1
    The answers you have received are a better way to implement this, but your first approach would work if you use \renewcommand{\theswot}{\protect\StrMid{SWOT}{\value{swot}}{\value{swot}}} (It also prints nothing at all for counter values > 4). – Alan Munn Aug 31 '19 at 12:05
  • Thanks, @AlanMunn, for that elucidation! Indeed the answers below didn't explain why my original attempt failed - now I know, something to watch out for in the future! – Arend Sep 1 '19 at 6:40
2

You can use the following template for that.

enter image description here

\documentclass{article}

\usepackage{expl3}
\ExplSyntaxOn
\newcommand{\Mod}[2]{\int_mod:nn{#1}{#2}}% https://tex.stackexchange.com/a/43209/5764
\ExplSyntaxOff

\newcounter{swot}

\renewcommand{\theswot}{%
  \ifcase\Mod{\value{swot}}{4}\relax
        T% 4
    \or S% 1
    \or W% 2
    \or O% 3
  \fi
}

\begin{document}

\stepcounter{swot}\theswot % 1 = S

\stepcounter{swot}\theswot % 2 = W

\stepcounter{swot}\theswot % 3 = O

\stepcounter{swot}\theswot % 4 = T

\stepcounter{swot}\theswot % 1 = S

\stepcounter{swot}\theswot % 2 = W

\stepcounter{swot}\theswot % 3 = O

\stepcounter{swot}\theswot % 4 = T

\end{document}

Modulo calculations is taken from How do I calculate n modulo 3 in LaTeX?.

  • Thanks, @Werner! In fact I used the answer in a simplified form since there's really no need for the modulo calculation - I can just do an \ifcase\value{swot} and leave the case for 0 open. – Arend Sep 1 '19 at 6:33
2

Define a proper interface to the numbering:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\cs_new:Nn \arend_swot:n
 {
  \int_case:nn { \int_mod:nn { #1 } { 4 } }
   {
    {1}{S}
    {2}{W}
    {3}{O}
    {0}{T}
   }
 }

\cs_new:Npn \fromswot #1 { \__arend_fromswot:c { c@#1 } }
\cs_new:Nn \__arend_fromswot:N { \arend_swot:n { #1 } }
\cs_generate_variant:Nn \__arend_fromswot:N { c }

\ExplSyntaxOff

\newcounter{swot}
\renewcommand{\theswot}{\fromswot{swot}}
\setcounter{swot}{1}

\begin{document}

\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot--%
\stepcounter{swot}\theswot

\end{document}

enter image description here

You can check that using \refstepcounter{swot} and a subsequent \label produces the expected result.

  • I'm impressed - this is pure magic! I took a shallow dive into xparse and expl3 to try and understand what's going on, but I can't say it all sunk in. Since I prefer an answer I actually understand I selected the simpler one above. – Arend Sep 1 '19 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.