# Changbars to indicate location of conditional text

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}


2011-07-15 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.

2011-07-21 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}%
\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}


The following version works using TikZ, and works in math mode as well as text. The idea is to use the [remember picture,overlay] options for the tikzpicture, which 1) takes up no space in your line of text, and 2) lets you use the current page node.

\usepackage{tikz}
\usetikzlibrary[calc,fit]
\newlength\togglelen
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {\vphantom{X}};}
\makeatletter
\newcommand\changebar[2]{%
\begin{tikzpicture}[remember picture,overlay]
\coordinate (one) at ($(first.north -| current page.south west)+(#1,0)$);
\coordinate (two) at ($(second.south -| current page.south west)+(#1,0)$);
\node[fit=(one) (two),fill=#2,minimum width=3pt,inner sep=0pt,anchor=base east] {};
\end{tikzpicture}%
}
\newcommand{\MyToggleT}{\@ifstar\MyToggleTT\MyToggleTN}
\newcommand{\MyToggleTN}[3]{%
\setlength{\togglelen}{\hoffset}%
\global\edef\temp{\the\togglelen}%  THIS IS A HACK TO AVOID GROUPING PROBLEMS WITH &
\iftoggle{#1}{%
\tikzmark{first}#2\tikzmark{second}%
\expandafter\changebar\expandafter{\temp}{red}%
}{%
\tikzmark{first}#3\tikzmark{second}%
\expandafter\changebar\expandafter{\temp}{blue}%
}%
}%

\newcommand{\MyToggleTT}[3]{\MyToggleTN{#1}{#2}{#3}\\}%
\makeatother


Using this macro requires two compilations to reach a stable output. Note that if you set \togglefalse{paper}, then your second example will produce a blank line, because the \\ is not encased in the MyToggleT macros...but the changebars will still appear correctly.

Update 7-16-11: According to the new challenge in the question, the change bars don't just need to indicate the first line that's conditional, but rather all of them. But as the question stands, I don't think it's possible to detect precisely the height of the argument, because \\ is included in your argument, and if you exclude it, then the blank link will always appear.

Update 7-20-11: Added a simple starred variation that would insert the \\ for you, so you'd get a more precise estimate of the height (turns out the errors I had before were from forgetting the \makeatletter for the preamble of a tex document instead of a style file...)

In any case, the version above produces change bars that are multiple lines tall. It uses Martin Scharrer's \tikzmark macro to mark coordinates before and after the argument, then draws a change bar that fits around them. As a result, this version is less robust than the previous one, and will fail if the argument gets typeset to straddle a page boundary.

Update 7-22-11: The macro appeared broken because the \togglelen length was getting reset to 0.0pt. After some digging, this appears to be happening because of the & in the math argument to \MyToggleT. See this answer for a short and sweet explanation of what the & is actually doing... I can't resolve that by wrapping the argument in a group, because that breaks the math mode. So instead I used \global\edef\temp{\the\togglelen}, which is sub-optimal but sidesteps the issue.

• This does not produce ANY change bars for me, even in the simplest case. I am using the latest updated version of TexLive 2010, so not sure what the problem is. – Peter Grill Jul 16 '11 at 8:07
• @PeterGrill -- whups! I deliberately edited this yesterday to change from "south west" to "south east", when I meant to change the anchor of the nodes instead... As written it places the change bars just off the right edge of the page. Fixed. – Ben Lerner Jul 16 '11 at 17:13
• See the last two examples I added to the MWE. It appears that the changes are only indicated on the first line not the entire conditional text. – Peter Grill Jul 17 '11 at 2:26
• Not sure why I never got notified of you update so only saw this today. It seems that this version works pretty well except for the the problem you mentioned with the \. I don't know how to detect the new line at the end so not sure how to adjust for it. I do like the idea of having a starred version that adds the \, but have not yet been able to get that to work. – Peter Grill Jul 20 '11 at 20:45
• @PeterGrill, glad it works for you. I just updated again, once I remembered to \makeatletter so I could use \@ifstar... This version should give you the line-height behavior you want. – Ben Lerner Jul 20 '11 at 21:40