I'm trying to write a macro to let me write logic formulae in a more natural syntax. I've come up with a working version which however has a problem when used inside align environments (and in tabular as well).
This is the code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}% for \Box
\usepackage{xparse}
\usepackage{xstring}
\usepackage{pgffor}
\makeatletter
\NewDocumentCommand\logic{m}{%
\def\@formula{#1}%
\saveexpandmode
\expandarg
\foreach \keyword/\operator in \@operators {%
\StrSubstitute{\@formula}{\keyword}{\operator}[\@temp]%
\global\let\@formula\@temp
}%
\restoreexpandmode
\@formula
}
% Here the list of recognised syntax with the corresponding commands
\def\@operators{
[]/\Box,
<>/\Diamond,
!/\neg,
&&/\land,
||/\lor,
<->/\leftrightarrow,
->/\rightarrow,
<-/\leftarrow
}
\makeatother
\begin{document}
Here is a valid modal logic formula: $\logic{[]p -> p && <>[]p}$
This is a display modal logic formula:
\begin{align}
\logic{[]p -> p && <>[]p}
\end{align}
\end{document}
If I comment the second paragraph, the output is the following:
However, the same macro used inside the align environment gets totally mad, producing hundreds of error messages.
I was expecting problems when using & which is an active character with special meaning in align. I suppose the problem is that align splits beforehand the content of the line into two parts, \logic{[]p -> p and <>[]p}, which then cause all the troubles. Is that right?
Indeed, if I avoid using & in the formula and comment out the &&/\land line in the @operators macro, the macro works inside align as well.
Note that everything works in equation, using & as well.
Is there a way to make this code work inside align? Or is there a way to achieve the same result that works inside align?




\global\let\@formula\@temp, you should also do\gdef\@formula{#1}. – egreg Sep 25 '18 at 16:45