I am using etoolbox
as per this question on LaTeX conditional expression, and would like to modify the conditional to print some sort of indicator (like a vertical change bar in the margin) to show that the text displayed is conditional and subject to change. In the final version I intend to remove these change bars, but until then these indicators would be helpful in reviewing the document.
Other acceptable alternatives would be to underline, or highlight the conditional text, but think that that might be too distracting.
Based on Putting a bar in the margin I attempted to use changebar
, framed
, and pdfcomment
, but can't seem to get them to work. I think using the framed
package is not an option as that seems to insert a new line in paragraph mode. I was able to get the changebar
version working in text mode, but can't get any one of them to work in math mode.
Also, within an align*
, I can use \iftoggle
just fine (with an alignment character in between, ie as part of one of the arguments to \iftoggle
), but can't seem to get on of the macros that are \iftoggle
with some additional functionality working.
\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage[color]{changebar}
\usepackage{framed}
\usepackage[subject={Top1},author={Peter}]{pdfcomment}
\newtoggle{paper}
\toggletrue{paper}
\newcommand{\MyToggleC}[3]{% Try {changebar} package
\iftoggle{#1}{%
\cbcolor{red}\cbstart#2\cbend%
}{%
\cbcolor{blue}\cbstart#3\cbend%
}%
}%
% This seems to insert a new line, so probably can't use this
\newcommand{\MyToggleF}[3]{% Try {framed} package
\iftoggle{#1}{%
\begin{leftbar}%
#2%
\end{leftbar}%
}{%
\begin{leftbar}%
#3%
\end{leftbar}%
}%
}%
\newcommand{\MyToggleP}[3]{% Try {pdfcomment} package
\iftoggle{#1}{%
\begin{pdfsidelinecomment}[color=red,icolor=blue,caption=inline,linebegin={/Butt},lineend={/Square},linewidth=2bp,linesep=1cm]{Example}%
#2%
\end{pdfsidelinecomment}%
}{%
\begin{pdfsidelinecomment}[color=red,icolor=red,caption=inline,linebegin={/Butt},lineend={/Square},linewidth=2bp,linesep=1cm]{Example}%
#3%
\end{pdfsidelinecomment}%
}%
}%
\begin{document}
% This part works only with the {changebar} version: \MyToggleC
This document is intended for
\MyToggleC{paper}{paper}{electronic}
distribution.
% Can't get this to work with any version (ie can't get any change bars to show)
\begin{align*}
a &= b\\
\MyToggleC{paper}{c}{}&\MyToggleC{paper}{= d}{}\\
e &= f\\
\end{align*}
% But would prefer something like this as this won't leave a blank line
% if the toggle is disabled. This now works with the updated \MyToggleC
% but the changebars are not shown.
\begin{align*}
a &= b\\
\MyToggleC{paper}{
c &= d\\
}{}
e &= f\\
\end{align*}
This should highlight the first two lines, not just the first line
\begin{align*}
f(x)=
\begin{cases}
\MyToggleT{paper}{
-1 &\text{ for } x < 0\\
0 &\text{ for } x = 0\\
}{}
1 &\text{ for } x > 0
\end{cases}
\end{align*}
This should highlight four lines of this, not just one
\begin{align*}
f(x)
&={-\frac{7}{1.5} -{1}}+{\frac{7}{3}-{5}}\\
\MyToggleT{paper}{
&= -\frac{14}{3} -\frac{3}{3} +\frac{7}{3}-\frac{15}{3}\\
&=-\frac{17}{3} -\frac{8}{3}\\
&=\frac{-17-8}{3}\\ &=\frac{-25}{3}\\
}{}
&=-8\frac{1}{3}
\end{align*}
\end{document}
2025-08-07 Update: The macro \MyToggleC changed to use the macros instead of the changebar
environment. This seems to work, better in that the conditional text is properly displayed, but still can not get the changebars to show within the align
environment. So, now it seems that my only problem is to get the changebars to display within the math environments.
Ideally I would prefer to have a macro such as \ifpaper
, \ifelectronic
which both take one parameter and enable printing of the text based instead of having to have an empty {}
for the else
clause. Epsecially important is that within the align
environment, only the particular lines get flagged, not the entire align
environment.
2025-08-07 Update
The solution provided seems to work great, but ONLY if it is used in text mode first before it is used in math mode. Without using in text mode first, the change indicators disappear on the second run (they show up in the first run, but not the second). In the MWE example below, things look great on the second run, but not if the def\PrintTextLineWithChanges
is commented out:
\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,fit}
\newlength\togglelen
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {\vphantom{X}};}
\makeatletter
\newcommand{\MyToggleT}{\@ifstar\MyToggleTT\MyToggleTN}
\newcommand{\MyToggleTN}[3]{%
\setlength{\togglelen}{\hoffset}%
\addtolength{\togglelen}{1in}%
\addtolength{\togglelen}{\oddsidemargin}%
\addtolength{\togglelen}{\leftskip}%
\iftoggle{#1}{%
\tikzmark{first}#2\tikzmark{second}%
\begin{tikzpicture}[remember picture,overlay]
\coordinate (one) at ($(first.north -| current page.south west)+(\togglelen,0)$);
\coordinate (two) at ($(second.south -| current page.south west)+(\togglelen,0)$);
\node[fit=(one) (two),fill=red,minimum width=3pt,inner
sep=0pt,anchor=base east] {};
\end{tikzpicture}%
}{%
\tikzmark{first}#3\tikzmark{second}%
\begin{tikzpicture}[remember picture,overlay]
\coordinate (one) at ($(first.north -| current page.south west)+(\togglelen,0)$);
\coordinate (two) at ($(second.south -| current page.south west)+(\togglelen,0)$);
\node[fit=(one) (two),fill=blue,minimum width=3pt,inner
sep=0pt,anchor=base east] {};
\end{tikzpicture}%
}%
}%
\newcommand{\MyToggleTT}[3]{\MyToggleTN{#1}{#2}{#3}\\}%
\makeatother
\newtoggle{paper}
\toggletrue{paper}
\def\PrintTextLineWithChanges{}% Nothing gets flagged with this commented.
\begin{document}
The lines with an $\oplus$ should be flagged.
\ifdefined\PrintTextLineWithChanges
Need text to use the MyToggleT macro here
otherwise things don't work
\MyToggleT{paper}{some text}{some other text}
\fi
\begin{align*}
f(x)=
\begin{cases}
\MyToggleT*{paper}{
-1 &\text{ for } x < 0 \oplus\\
0 &\text{ for } x = 0 \oplus
}{}
1 &\text{ for } x > 0
\end{cases}
\end{align*}
This should highlight four lines indicated with $\oplus$
\begin{align*}
f(x)
&= a\\
\MyToggleT*{paper}{
&= b\oplus\\
&= c\oplus\\
&= d\oplus\\
&= e\oplus
}{}
&=f
\end{align*}
\end{document}