In my documents I have conditional text, which is underlined (in non-display math mode) to make it more obvious to the reader as to exactly which portions of text is conditional text. In display math mode, I don't attempt to underline things, but have separate markers to indicate the conditional text. This is the purpose of the \ConditionalText macro below, which is a greatly simplified version of my actual use case.
So, I would like to be able to pass in text which starts in text mode but may have display math mode in it, and
- have the underlining skip the display math portion, and then
- resume after the display math.
The MWE below runs fine, but that is because the last \ConditionalText is commented, and that is the specific problem case.
Note:
The reason why I can not simply apply
\ConditionalTextthree times: once before the display math, then in the display math, and then outside the display math is illustrated in the full test case below with the setting\def\ShowProblemSpacingIssue{}I feel as if the Full Text Case below may not be all that clear as I had intended, so that can be ignored for now -- only relevant if one is interested in why I can't just apply the
\ConditionalTextmacro several times.One solution would be to manually adjust the spacing for the case when the text is being suppressed, but not only is that very error prone but also difficult to know how much space to adjust as there many be multiple display math environments, or
alignblocks composed of several lines of equations.Even though the MWE uses the
soulpackage, I am not stuck on that, so a\tikzmarktype of solution would be fine.Now that I have composed this question I feel as if this might be a classic x-y problem as I am really looking for a way to show conditional text which could be a few words, a few lines in the middle of a paragraph, be entire paragraphs possibly containing display math, or be portions of a display math equations, or even an entire display math environment. But the solution to this problem would require the least amount of change to my code, and hence am looking into this first.
References:
- I am using the solution from Mark portions of text without affecting spacing to mark the indicators fo the conditional text.
- Changbars to indicate location of conditional text
Code: MWE
\documentclass{article}
\usepackage{xparse}% Not essential for this problem.
\usepackage{xcolor}
\usepackage{amsmath}% just for \text{}
\usepackage{soul}
\NewDocumentCommand{\UnderlineText}{O{red} m}{%
\setulcolor{#1}\ul{#2}%
}%
\NewDocumentCommand{\ConditionalText}{O{red} m}{%
\ifmmode%
#2% Don't underline if in math mode
\else%
\UnderlineText[#1]{#2}%
\fi%
}%
\begin{document}
\noindent\ConditionalText{Underlining in text mode works great.}
\noindent\ConditionalText{Underlining works with inline math: $e = mc^2.$}
%
\[
\ConditionalText{F = ma \quad\text{Properly skips underling in math mode}}
\]
\noindent
%\ConditionalText{%
The problem is here when there is display math mode
\[ e^{\pi i} = -1\]
in the same paragraph where I need the underlining to \emph{skip}
the display math portion.
%}%
\end{document}
Code: Full Test Case
% With the following commented, I should not need the
% \renewcommand that gets executed below.
\def\ShowProblemSpacingIssue{}%
\documentclass{article}
\usepackage{xparse}% Not essential for this problem.
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{amsmath}% just for \text{}
\usepackage{soul}
\usepackage{parskip}
\usepackage{showframe}
\newtoggle{SupressConditionalText}
\ifdefined\ShowProblemSpacingIssue
\toggletrue{SupressConditionalText}
\else
\togglefalse{SupressConditionalText}% This requires last use of \ConditionalText to be commented
\fi
\NewDocumentCommand{\UnderlineText}{O{red} +m}{%
\setulcolor{#1}\ul{#2}%
}%
\NewDocumentCommand{\ConditionalText}{O{red} +m}{%
\ignorespaces%
\iftoggle{SupressConditionalText}{}{%
\ifmmode%
#2% Don't underline if in math mode
\else%
\UnderlineText[#1]{#2}%
\fi%
}%
\ignorespacesafterend%
}%
\begin{document}
\ConditionalText{Underlining in text mode works great.}
\ConditionalText{Underlining works with inline math: $e = mc^2.$}
%
Text before a conditional display math formula.
\[
\ConditionalText{F = ma \quad\text{Properly skips underling in math mode}}
\]
Text after a conditional display math formula.
The following works in terms of toggling the conditional text, but has a spacing issue due to the conditional text in the display math environment:
%
\ConditionalText{%
The problem is here when there is display math mode}%
\[ \ConditionalText{e^{\pi i} = -1} \]
\ConditionalText{%
in the same paragraph where I need the underling to \emph{skip}
the display math portion.
}%
\ifdefined\ShowProblemSpacingIssue
\else
% Want to be able to eliminate this \renewcommand,
% and use \ConditionalText as below.
\renewcommand{\ConditionalText}[2][red]{%{O{red} +m}{%
\iftoggle{SupressConditionalText}{}{%
#2% Don't underline if in math mode
}%
}%
\fi
\ConditionalText{%
The problem is here when there is display math mode
\[ e^{\pi i} = -1\]
in the same paragraph where I need the underling to \emph{skip}
the display math portion.
}%
Some text at end of document to see placement.
Note the additional space above this with
\verb|\def\ShowProblemSpacingIssue{}|.
\end{document}

soulcan cope with display math mode. – egreg Feb 19 '13 at 22:21soulsolution. – Peter Grill Feb 19 '13 at 22:29