TransWikia.com

How to use ifdefined inside align*

TeX - LaTeX Asked on January 15, 2021

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} &Longrightarrowhspace{5pt}
ifdefinedHCode
   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

2 Answers

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} &Longrightarrowhspace{5pt}
{ifdefinedHCode
   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} &Longrightarrowhspace{5pt}
left[
begin{array}{@{}ccc!{ifdefinedHCode |elsecolor{red}vline width 0.6ptfi}c@{}}
                       0&0&4&0 
                       0&0&0&0 
                       0&0&2&0 
          end{array}
    right]               
end{align*}
end{document}

Correct answer by David Carlisle on January 15, 2021

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} &Longrightarrowhspace{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}

Answered by Ulrike Fischer on January 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP