[Tex/LaTex] How to format a regular expression in math mode

math-mode

I want to display some regular expression, like the following, in LaTeX math mode.

\[
(F+(A+I*)*)|(F*(A+I*)+)E
\]

However, the spacing is all wrong, because TeX formats + and * as binary operators, rather than as postfix ones. I would expect that the postfix operators would appear close to the preceding operand, and, in cases where the successor element is an operand, they would introduce some additional space to prevent the appearance's ambiguity.

An example formatted in the way I'd like it to be appears below. I have marked with red the places where additional spacing is unwanted and with green places where additional spacing is required. However, I'd be open to other formatting suggestions from typography experts.

enter image description here

Is there a way to handle this without adding manual spacing commands?

Best Answer

It would be possible to set up +*? to act automatically as postfix, but how easy it is depends a bit if they also need their normal definitions within the same document or same expression.

Probably simplest is to set up simple commands for the postfix versions for example:

enter image description here

\documentclass{article}

\makeatletter

\def\+{\@postfix+}
\def\*{\@postfix*}
\def\?{\@postfix?}



\def\@postfix#1{{#1}\@ifnextchar){}{\;}}

\makeatother
\begin{document}

\[
(F+(A+I*)*)|(F*(A+I*)+)E
\]


\[
(F\+(A\+I\*)\*)|(F\*(A\+I\*)\+)E
\]


\end{document}
Related Question