3

I am writing a document that contains some mathematical proofs. I would like to define a command in LaTeX that enables me to write some additional passages to my proofs which are shown only when a certain option is enabled. Moreover, these additional passages should have a different color from the rest of the text.

\documentclass{article}
\usepackage{xcolor}

\usepackage{amsmath}

\usepackage{etoolbox}
\providetoggle{detail}
\settoggle{detail}{true}

\newcommand{\note}[2]{\iftoggle{detail}{
        \color{red} #1 }{#2}}


\begin{document}
\begin{align}
a  & = \note{b \\ 
    & = c \\ 
    & = d \\ 
    & = } e. 
\end{align}
\end{document}

The code above is a minimal example. LaTeX is compiling as expected, but the color applies only to "b" and not to the other terms contained in the \note.

Sam
  • 277

3 Answers3

3

If you are willing to use a slightly altered syntax inside of \note, that is \& instead of &, this might work:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{etoolbox}
\providetoggle{detail}
\def\&{&\color{red}}
\def\note#1{\iftoggle{detail}{\color{red}#1\color{black}}{}}

\begin{document}
\settoggle{detail}{true}
\begin{align}
a  & = \note{ b \\ 
    \& = c \\ 
    \& =  d \\ 
    \& = } e. 
\end{align}
\settoggle{detail}{false}
\begin{align}
a  & = \note{ b \\ 
    \& = c \\ 
    \& =  d \\ 
    \& = } e. 
\end{align}
\end{document}

enter image description here

  • When I set "detail" to false I want to hyde the passagese marked as notes. – Sam Nov 21 '19 at 16:01
  • @Sam Please see my revision. – Steven B. Segletes Nov 21 '19 at 16:37
  • Hi, I like your solution, it is simple, and works with minimal modification of the latex code. However, according to my taste, the solution provided by Schrodinger's cat is more elegant and clean, and therefore it is the correct solution to my question. I anyway positively marked your answer! Thanks for your help and time! – Sam Nov 22 '19 at 10:34
3

I am not sure if this is an infinitely stable solution, but it seems to work so far. The idea is to change \everymath. With a little help from here, I arrived at

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{etoolbox}
\providetoggle{detail}
\settoggle{detail}{true}
\newcommand{\note}[2]{\iftoggle{detail}{%
\gdef\mycolor{red}%
\color{red}
#1 \gdef\mycolor{black}\color{black}
}{#2}}
\everymath\expandafter{\the\everymath\color{\mycolor}}  
\def\mycolor{black}
\begin{document}
\begin{align}
a  & = \note{b \\ 
    & = c \\ 
    & = d \\ 
    & = e} e. 
\end{align}

\settoggle{detail}{false}

\begin{align}
a  & = \note{b \\ 
    & = c \\ 
    & = d \\ 
    & = e} e. 
\end{align}

\end{document}

enter image description here

One could make it presumably more tidy. It might be cleaner to use \colorlet but I was not able to make that global (or to smuggle it out of the group) except for using \globaldefs1 but this is something that is not to be used so I used macro.

1

I take that you wish to insert some stuff when the toggle is true and this stuff should be red. When the toggle is false, the stuff should not be visible.

The problem here is that \color only reaches until the end of the current box, and each cell in an align (or any other array) environment represents such a box.

Hence, you need to somehow tell LaTeX at the beginning of each following box (cell) to select the color. You could solve your problem perhaps as follows:

\documentclass{article}

\usepackage{amsmath,etoolbox,xcolor}
\providetoggle{detail}
\settoggle{detail}{false}

\newcommand{\note}[2]{\iftoggle{detail}{#1 \color{red} #2 \color{black}}{}}

\begin{document}

\begin{align}
    a & = \note{}{ b \\ } 
    \note{ & }{ = c \\ }
    \note{ & }{ = d \\ }
    \note{ & }{ = } e 
\end{align}

\end{document}

If you set the toggle to true, the following will be output:

enter image description here

And this is the result when the toggle is set to false:

enter image description here

However, while this works with this quite simple example, it may not work in other, more complex situations …

  • Hi, thanks for your answer. I am looking forward to something more elegant! This forces me to write multiple times \note. I also think is usually better to use \normalcolor instead of color{black}. – Sam Nov 20 '19 at 17:49
  • I know that this is not the final answer. I just wanted to point out some problems that arise … I think, someone else will come up with a nice solution. – Jasper Habicht Nov 20 '19 at 17:54