5

I have a special kind of variable in my equations which I want to be formatted dependent on its case (lower or upper).

I already tried using the package stringstrings, but there seem to be problems with math mode and grouping braces. In the example below it always chooses the then-part, ignoring whether the variable is lowercase or not.

\documentclass{article}
\usepackage{stringstrings}
\usepackage{amssymb}

\newcommand{\testStringstrings}[1]{\testlowercase{#1}\iflowercase{is lowercase}\else{is uppercase}\fi}

\newcommand{\specialvar}[1]{\testlowercase{#1}\iflowercase{\mathrm{#1}}\else{\mathbb{#1}}\fi} % always called within math-mode

\begin{document}

    % works fine
    t \testStringstrings{t} and T \testStringstrings{T}

    % does not work as expected (T should be formatted with \mathbb but is in \mathrm)
    \(\specialvar{t} \in \specialvar{T}\)

\end{document}

Is there a nice solution to solve this problem?

8

Everything weird will happen if you have more than one letter in the argument of \specialvar or something that's not a letter, but here it is:

\documentclass{article}
\usepackage{amssymb}

\newcommand{\specialvar}[1]{%
  \ifnum\lccode`#1=`#1
    \mathrm{#1}%
  \else
    \mathbb{#1}%
  \fi
}

\begin{document}

\(\specialvar{t} \in \specialvar{T}\)

\end{document}

enter image description here

There seems to be a bug in stringstrings, because with

\testlowercase{A}\iflowercase\LOWER\else\UPPER\fi
$\testlowercase{A}\iflowercase\LOWER\else\UPPER\fi$

LaTeX stops with

! Undefined control sequence.
l.12 ...tlowercase{A}\iflowercase\LOWER\else\UPPER
                                                  \fi

in the first case, but with

! Undefined control sequence.
l.13 $\testlowercase{A}\iflowercase\LOWER
                                         \else\UPPER\fi$

in the second case.


A workaround using stringstrings, which however adds an empty ordinary atom to the math formula (no side effect should arise, though):

\documentclass{article}
\usepackage{stringstrings}
\usepackage{amssymb}

\newcommand{\specialvar}[1]{%
  \mbox{\testlowercase{#1}\expandafter}\iflowercase \mathrm{#1}\else\mathbb{#1}\fi
} % always called within math-mode

\begin{document}

\(\specialvar{t} \in \specialvar{T}\)

\end{document}

The conditional is expanded before the empty box is finished, but the truth value is known.

A different workaround that doesn't add a math atom uses a temporary box and \aftergroup.

\documentclass{article}
\usepackage{stringstrings}
\usepackage{amssymb}

\newcommand\testmlowercase[1]{%
  \sbox0{%
    \testlowercase{#1}%
    \iflowercase
      \aftergroup\lowercasetrue
    \else
      \aftergroup\lowercasefalse
    \fi
  }%
}

\newcommand{\specialvar}[1]{%
  \testmlowercase{#1}\iflowercase \mathrm{#1}\else\mathbb{#1}\fi
} % always called within math-mode

\begin{document}

\(\specialvar{t} \in \specialvar{T}\)

\end{document}
  • Thanks for performing diagnosis on stringstrings. Perhaps the patient is terminal 8^). – Steven B. Segletes Jan 28 '15 at 14:39
4

UPDATE: The bug in the stringstrings package that prompted the OP's query has been resolved in V1.23, uploaded to CTAN on 2 FEB 2015. The answer below (developed prior to V1.23) provides the workaround for use with earlier versions of the package.


It's been too long since I looked at the code, but it seems that the test does not work in math mode, so to fix it for this case, I stuffed it in an \mbox and saved an intermediate flag.

And in case one wonders why I resort to an intermediate flag \tmp, it is to keep the actual output outside of the \mbox, which will allow it to take on the current mathstyle, as shown in the EDITED MWE.

\documentclass{article}
\usepackage{stringstrings}
\usepackage{amssymb}

\newcommand{\testStringstrings}[1]{\testlowercase{#1}\iflowercase{is lowercase}\else{is uppercase}\fi}

\newcommand{\specialvar}[1]{%
  \mbox{\testlowercase{#1}\iflowercase{\gdef\tmp{T}}\else\gdef\tmp{F}\fi}%
  \if T\tmp\mathrm{#1}\else\mathbb{#1}\fi%
} % always called within math-mode

\begin{document}

    % works fine
    t \testStringstrings{t} and T \testStringstrings{T}

    % now works as expected (T should be formatted with \mathbb and it is)
    \(\specialvar{t} \in \specialvar{T}\)
    \(\scriptstyle\specialvar{t} \in \specialvar{T}\)
    \(\scriptscriptstyle\specialvar{t} \in \specialvar{T}\)

\end{document}

enter image description here

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.