Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 \ConditionalText three 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 \ConditionalText macro 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 align blocks composed of several lines of equations.

  • Even though the MWE uses the soul package, I am not stuck on that, so a \tikzmark type 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:


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}
share|improve this question
    
I don't think soul can cope with display math mode. –  egreg Feb 19 '13 at 22:21
    
@egreg: Hence the desire to skip the display math portion, or perhaps a non-soul solution. –  Peter Grill Feb 19 '13 at 22:29
    
I'm not sure how to understand your spacing issue. Do you want that the spacing doesn't change if you suppress the conditional text? That doesn't work with pure text either in your Full Test Case. –  Hendrik Vogt Feb 20 '13 at 13:28
    
@HendrikVogt: Sorry for not being clear. The spacing should change with the conditional text being suppressed. Have corrected the MWE to proper;y illustrate that at least for text (on inline math) mode. But, the problem is that I am not and to apply the conditional to the entire display math environment (because of the behavior I want in text mode). So, the spacing without conditional text should be as if the conditional text was not in the document. –  Peter Grill Feb 20 '13 at 18:20
    
@Peter: Thanks, I guess I got it: You've got an empty math display if you don't show the conditional text, and this gives some extra space you don't want - correct? –  Hendrik Vogt Feb 21 '13 at 9:34

1 Answer 1

I'm not encouraged that my solution, as it is, can solve your problem for the general case, but it may give you some ideas on how to proceed. It uses the stringstrings package to parse the input line looking for display math. It then deals with the pre-display text first, then with the display math, and then with the post-display-math text.

The problem is that stringstrings is not set up to handle math mode special characters, per se, though it does have limited ability in this regard. I have employed that limited capacity to add \[, \], and \pi to the stringstrings vocabulary using the \encodetoken command. Unfortunately, the package only allows three such assignments at any given time (which is why this solution is not yet general). Furthermore, stringstrings cannot handle { or } grouping symbols in its argument, though it has a workaround if you use \LB and \RB in their stead. And finally, in math mode, stringstrings may inadvertantly add some extra glue, which can mess up your spacings.

With those disclaimers said, the package was able to handle your particular case. Note that I used red text instead of underline, because your \UnderlineText command broke when I tried to use it in my solution.

Anyhow, here it is:

\documentclass{article}
\usepackage{xparse}% Not essential for this problem.
\usepackage{xcolor}
\usepackage{amsmath}% just for \text{}
\usepackage{soul}
\usepackage{stringstrings}

\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%
}%
\newcounter{letpos}
\newcounter{letpoe}
\newcommand\myCT[1]{%
% PREPARE
  \encodetoken[1]{\[}%
  \encodetoken[2]{\]}%
  \encodetoken[3]{\pi}%
% PRE MATH MODE
  \whereisword[q]{#1}{\[}%
  \setcounter{letpos}{\theresult}%
  \addtocounter{letpos}{-1}%
  \substring[q]{#1}{1}{\theletpos}%
  \textcolor{red}{\thestring}%
% MATH MODE
  \addtocounter{letpos}{1}%
  \whereisword[q]{#1}{\]}%
  \setcounter{letpoe}{\theresult}%
  \substring[e]{#1}{\theletpos}{\theletpoe}%
  \retokenize[v]{\thestring}%
% POST MATHMODE
  \addtocounter{letpoe}{1}%
  \substring[q]{#1}{\theletpoe}{$}%
  \removeleadingspaces[q]{\thestring}%
  \textcolor{red}{\thestring}%
% CLEAN UP
  \decodetoken[1]{\[}%
  \decodetoken[2]{\]}%
  \decodetoken[3]{\pi}%
}
\begin{document}
\noindent
\myCT{%
The problem is here when there is display math mode 
\[ e^\LB\pi i\RB =-1\]
in the same paragraph where I need the underlining to skip
the display math portion. And here is some inline math, $x=3$, as an
extra.
}
\end{document}

and here is what the output looks like:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.