2

I have the two following macros that I use to add some kind of "revision" to my latex files:

\newcommand\rem[2][1]{
  \ifdefined\revision
    \ifnum \revision = 0
      \textcolor{red}{\sout{#2}}
    \else
      \ifnum \revision < #1 #2 \fi
      \ifnum \revision = #1 \textcolor{red}{\sout{#2}} \fi
    \fi
  \fi
}

\newcommand\remb[2][1]{
  \ifdefined\revision
    \ifnum \revision = #1 #2 \fi
    \ifnum \revision < #1 #2 \fi
  \fi
}

Basically, \rem is used to indicate stuff that has been removed and \remb is used to remove block of code that cannot be removed with \rem, a typical use (that works):

 % Code that works
\remb[2]{ % In revision 2, the whole list was removed.
  \begin{itemize}
  \item \rem[2]{A}
    \remb[1]{ % In revision 1, the two last items were removed.
    \item \rem[1]{B}
    \item \rem[1]{C}
    }
  \end{itemize}
}

I want to use the above in an align environment to remove part of equations, but it does not work:

\begin{subequations}
  \begin{align}
    \remb{A & B\\} % Small code that does not work
            & G
  \end{align}
\end{subequations}

\begin{subequations} % "Complete" code
  \begin{align}
    \remb{
    \rem{A} & \rem{B}\\
    \rem{C}
    } & D\\
    \remb{
      & \rem{E}\\
      & \rem{F}\\
    }
    & G
  \end{align}
\end{subequations}

Without the \ifdefined and \ifnum inside the \rem and \remb macro (e.g. by conditionally defining the macro), it works. Is there a way to make it work with the conditional statement inside?

Error I get with the above code:

! Incomplete \ifnum; all text was ignored after line 51.
<inserted text>
                \fi
l.51   \end{align}

Full code:

\documentclass{article}

\usepackage{amsmath,amsthm,amsfonts,amssymb}

% Works only if revision is 1
\def\revision{2}

\newcommand\remb[2][1]{
  \ifdefined\revision
  \ifnum\revision=#1 #2\fi
  \ifnum\revision<#1 #2\fi
  \fi
}

\begin{document}

\begin{subequations}
  \begin{align}
    \remb{A & B\\}
            & G
  \end{align}
\end{subequations}

\end{document}
4
  • 1
    I don't think subequations has anything to do with this as it just messes with counters. I'm assuming align cannot see the & inside the \remb, what exactly are you trying to do inside the math here?
    – daleif
    Commented Sep 26, 2016 at 7:40
  • @daleif I want to remove the first line and the content of the first column of the second line in my align environment, plus some other lines - It was working before I added the \if in the command (I will add the error I get to the post).
    – Holt
    Commented Sep 26, 2016 at 7:44
  • Doing stuff like this inside tabular-like environments often call for very nasty parsing tricks (to fool the parser that looks for &) and sometimes it is just not possible.
    – daleif
    Commented Sep 26, 2016 at 8:22
  • @daleif I am open to a alternative if you have any - Everything was working fine when I had only one level of revision (I did not have \if inside \remb).
    – Holt
    Commented Sep 26, 2016 at 8:31

1 Answer 1

3

Using conditionals that way in conditionals is always risky and usually doesn't work, because conditionals cannot straddle cells.

Hide the conditionals until the last moment.

\documentclass{article}

\usepackage{amsmath,amsthm,amsfonts,amssymb}

\makeatletter
\newcommand{\revisionTF}[2]{%
  \ifdefined\revision
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#1}{#2}%
}
\newcommand{\revisionT}[1]{\revisionTF{#1}{}}
\newcommand{\revisionF}[1]{\revisionTF{}{#1}}

\newcommand{\revisioncompareTF}[3]{%
  \ifnum\revision#1\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#2}{#3}%
}

\newcommand\remb[2][1]{%
  \revisionTF
    {%
     \revisioncompareTF{=#1}
       {EQUAL #2}
       {\revisioncompareTF{<#1}{LESS #2}{}%
     }%
    }%
    {NO REV}
}

\begin{document}

Revision is undefined:
\begin{align}
\remb{A & B\\}
  & G
\end{align}

\def\revision{1}
Revision is 1:
\begin{align}
\remb{A & B\\}
  & G
\end{align}

\def\revision{2}
Revision is 2:
\begin{align}
\remb{A & B\\}
  & G
\end{align}

\def\revision{2}
Revision is 2:
\begin{align}
\remb[3]{A & B\\}
  & G
\end{align}

\end{document}
3
  • This works! Will dig into the code to try understand it now ;)
    – Holt
    Commented Sep 26, 2016 at 9:02
  • @Holt I added, but not used, \revisionT and \revisionF, shorthands for when you just have one branch to follow.
    – egreg
    Commented Sep 26, 2016 at 9:12
  • I think I understand most of your code. I have replaced the nested \revisioncompareTF by a single \revisioncompareTF{>#1}{}{#2} which (I think) is equivalent. Thanks!
    – Holt
    Commented Sep 26, 2016 at 9:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .