[Tex/LaTex] Why won’t the boolean work correctly

etoolbox

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.

Best Answer

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

enter image description here

\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}