Within an align*
environment I have lines of the form
\Rightarrow &<\texttt{Program Code}, [a \mapsto 1, b \mapsto 2]> &\text{Rule Name}\\
\Rightarrow &<\texttt{Program Code 2}, [a \mapsto 2, b \mapsto 3]> &\text{Rule Name 2} \\
Giving
I thought of making a macro to make this much easier, of the form
\pline{Program Code}{a \mapsto 2,b \mapsto 2}{Rule name}
I want the third argument, the rule name, to be optional. If so, it shouldn't show up on the line, on the right.
I was trying to use the native ifx
/else
combo but I switched to the xparse
package. Right now, I have this:
\NewDocumentCommand{\pline}{+m +m o}{
\Rightarrow &<\texttt{#1}, \ensuremath{[#2]}>
\IfValueTF{#3}
{&\text{#3}}
}
However, the rule names do not align right. See the below demonstration:
\begin{align*}
% Explicit Code
\Rightarrow &<\texttt{Program Code}, [a \mapsto 1, b \mapsto 2]> &\text{Rule Name} \\
\Rightarrow &<\texttt{Program Code 2}, [a \mapsto 2, b \mapsto 3]> &\text{Rule Name 2} \\\\
% Commands
\pline{Program Code}{a \mapsto 1,b \mapsto 2}{Rule Name} \\
\pline{Program Code 2}{a \mapsto 2,b \mapsto 3}{Rule Name 2}
\end{align*}
To fix this, I put the &
infront of \IfValueTF
and removed it from before the \text{#3}
. However, though this fixes the alignment issue, the font is still wrong and seems to stay in math mode, in spite of the \text
.
How can I fix both these issues?
Minimal working example:
\documentclass[notitlepage]{report}
\usepackage{mathpazo}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\begin{document}
\NewDocumentCommand{\pline}{+m +m o}{
\Rightarrow &<\texttt{#1}, \ensuremath{[#2]}>
\IfValueTF{#3}
{\&text{#3}}
}
\NewDocumentCommand{\plinealigned}{+m +m o}{
\Rightarrow &<\texttt{#1}, \ensuremath{[#2]}>
&\IfValueTF{#3}
{\text{#3}}
}
\begin{align*}
% Explicit Code
\Rightarrow &<\texttt{Program Code}, [a \mapsto 1, b \mapsto 2]> &\text{Rule Name} \\
\Rightarrow &<\texttt{Program Code 2}, [a \mapsto 2, b \mapsto 3]> &\text{Rule Name 2} \\\\
% Commands
\pline{Program Code}{a \mapsto 1,b \mapsto 2}{Rule Name} \\
\pline{Program Code 2}{a \mapsto 2,b \mapsto 3}{Rule Name 2}\\\\
% Commands with alignment fixed
\plinealigned{Program Code}{a \mapsto 1,b \mapsto 2}{Rule Name} \\
\plinealigned{Program Code 2}{a \mapsto 2,b \mapsto 3}{Rule Name 2}
\end{align*}
\end{document}
\IfValueTF
to\IfValueT
or add a pair of curly braces.\IfValueTF
takes three arguments: one for the thing to be tested, one for the true branch and one for the false branch. But in your defintion, you miss the latter. Actually, withO{}
instead ofo
, you can omit the test altogether.