2

I need to use \ifdefined inside align to tell latex to use different code for pdf and for tex4ht. But it gives an error.

Is there any way around this (other than putting the ifdefined outside everything, which will complicate things for me.

Here is MW

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{array}

\begin{document}

\begin{align*}    
R_{2} = R_{2}-R_{1} &\Longrightarrow\hspace{5pt}
\ifdefined\HCode
   \left[\begin{array}{@{}ccc|c@{}}
                       0&0&4&0\\ 
                       0&0&0&0\\ 
                       0&0&2&0 
          \end{array}
    \right]               
\else
     \left[             
         \begin{array}{@{}ccc!{\color{red}\vline width 0.6pt}c@{}}
                       0&0&4&0\\ 
                       0&0&0&0\\ 
                       0&0&2&0
         \end{array}
     \right]               
\fi      
\end{align*}
\end{document}

Compiling with lualatex gives error

(/usr/local/texlive/2020/texmf-dist/tex/latex/microtype/mt-PalatinoLinotype.cfg) (/usr/local/texlive/2020/texmf-dist/tex/latex/microtype/mt-msa.cfg) (/usr/local/texlive/2020/texmf-dist/tex/latex/microtype/mt-msb.cfg))
! Incomplete \ifx; all text was ignored after line 21.
<inserted text>
\fi
<*> foo3.tex

?

TL 2020

4
  • @campa it does not work for me. Did it work on your end? still same error.
    – Nasser
    Oct 28, 2020 at 12:50
  • It did, but I've got TeX Live 2017.
    – campa
    Oct 28, 2020 at 12:51
  • @campa I was using same code I have but added {}{} as you suggested but did not work. May be I did something wrong. But it works now using \ifdef{HCode} as shown in answer below. Thanks.
    – Nasser
    Oct 28, 2020 at 12:57
  • Thanks for all the answers. They are all very good and all worked.
    – Nasser
    Oct 28, 2020 at 13:24

2 Answers 2

3

You can put it in a group:

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{array}

\begin{document}

\begin{align*}    
R_{2} = R_{2}-R_{1} &\Longrightarrow\hspace{5pt}
{\ifdefined\HCode
   \left[\begin{array}{@{}ccc|c@{}}
                       0&0&4&0\\ 
                       0&0&0&0\\ 
                       0&0&2&0 
          \end{array}
    \right]               
\else
     \left[             
         \begin{array}{@{}ccc!{\color{red}\vline width 0.6pt}c@{}}
                       0&0&4&0\\ 
                       0&0&0&0\\ 
                       0&0&2&0
         \end{array}
     \right]               
\fi}     
\end{align*}
\end{document}

But I don't see why you repeat the matrix in each branch,

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{array}

\begin{document}

\begin{align*}    
R_{2} = R_{2}-R_{1} &\Longrightarrow\hspace{5pt}
\left[
\begin{array}{@{}ccc!{\ifdefined\HCode |\else\color{red}\vline width 0.6pt\fi}c@{}}
                       0&0&4&0\\ 
                       0&0&0&0\\ 
                       0&0&2&0 
          \end{array}
    \right]               
\end{align*}
\end{document}
4

Primitive \if commands are difficult. You often have to ensure that the \else and \fi are found before the branch is executed. Use a user command like \ifdef from etoolbox which hides this from you:

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{array}
\usepackage{etoolbox}

\begin{document}

\begin{align*}
R_{2} = R_{2}-R_{1} &\Longrightarrow\hspace{5pt}
\ifdef{\HCode}
   {\left[\begin{array}{@{}ccc|c@{}}
                       0&0&4&0\\
                       0&0&0&0\\
                       0&0&2&0
          \end{array}
    \right]}
   {\left[
         \begin{array}{@{}ccc!{\color{red}\vline width 0.6pt}c@{}}
                       0&0&4&0\\
                       0&0&0&0\\
                       0&0&2&0
         \end{array}
     \right]}
\end{align*}
\end{document}

You must log in to answer this question.

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