# Why won't my boolean work correctly?

by thisismynamenow   Last Updated July 08, 2016 08:01 AM

Sorry for the very open question title, but I can't really figure out what is wrong exactly. I wanted to save a little time when writing semantic types, which are usually subscript but are often recursive, leading to types within types, like this:

D<e,<s,t>>

The easiest solution seemed to me to use a boolean which checks whether I am within Type Brackets (<>) already; if so, \type would not use \textsubscript, if not, \type would set the boolean to true and use \textsubscript; the last command in \type would set the boolean to false if it had been false when the command was called. This is a minimal example:

\documentclass{article}
\usepackage{etoolbox}

\newbool{intype}
\newcommand{\type}[1]{\ifbool{intype}{$\langle$#1$\rangle$}{\setbool{intype}{true}\textsubscript{$\langle$#1$\rangle$}}\setbool{intype}{false}}

\begin{document}
D\type{\type{$\tau$,\type{$s,t$}},\type{$\tau$,\type{$s$,$t$}}}
\end{document}


The expected output is D<<τ,<s,t>>,<τ,<s,t>>.

The output I get is D<<τ,<s,t>>,<τ,<s,t>>.

This is really confusing - If my solution doesn't work, why isn't the <s,t> bit in the first pair of brackets lower than the τ?

Sorry for that long line of code btw, as far as I can tell, adding spaces or line breaks produces unwanted spaces in the output.

Tags :

You are setting it false too soon, but you can just use the grouping to set it back automatically:

\documentclass{article}
\usepackage{etoolbox}

\newbool{intype}
\newcommand{\type}[1]{%
\ifbool{intype}%
{$\langle$#1$\rangle$}%
{\textsubscript{\setbool{intype}{true}$\langle$#1$\rangle$}}%
}

\begin{document}
D\type{\type{$\tau$,\type{$s,t$}},\type{$\tau$,\type{$s$,$t$}}}
\end{document}

David Carlisle
July 08, 2016 12:08 PM

The boolean is set to false in the wrong place.

Here's a different implementation, based on grouping, that avoids code repetition. Since \textsubscript typesets its argument in a group, the “outer” definition of \type will be restored as soon as the subscript has been typeset.

\documentclass{article}

\newcommand{\type}[1]{%
\textsubscript{\let\type\innertype\innertype{#1}}%
}
\newcommand{\innertype}[1]{$\langle$#1$\rangle$}

\begin{document}
D\type{\type{$\tau$,\type{$s,t$}},\type{$\tau$,\type{$s$,$t$}}}
\end{document}


egreg
July 08, 2016 12:52 PM