TransWikia.com

How to cross a row in matrix with a line?

TeX - LaTeX Asked by Predrag Punosevac on June 7, 2021

Is there an “easy” way to cross the row in a matrix with a line? I am trying to demonstrate to my students how to type in LaTeX their homework assignment on Boolean function. The below table is used during the construction of normal conjunctive and disjunctive form of a Boolean function. This is a “hand” made example.

example table how it should look like

2 Answers

You can use tikz for this. Probably not the finest answer possible, but works for me:

documentclass{article}
usepackage{tikz}
newcommand{pmark}[1]{begin{tikzpicture}[overlay,remember picture]node(#1)at (-1em,.7ex){};end{tikzpicture}}
newcommand{smark}[1]{begin{tikzpicture}[overlay,remember picture]draw(#1)--(0,.7ex);end{tikzpicture}}
begin{document}
$$begin{array}{cccc|c}
a&b&bc&d&fhline
0pmark{a}&0&1&1&1smark{a}
0&0&0&1&0
0&0&1&1&1
end{array}$$
$f=overline{a}wedgeoverline{b}wedge cwedge+dots$
end{document}

Correct answer by Jean-Christophe Dubacq on June 7, 2021

With nicematrix, you can write a command Cross to use in each row you want to cross. You will be able to use that command in the environments of nicematrix ({NiceTabular}, {pNiceMatrix}, etc.) which are similar to the classical environments (of the packages array and amsmath).

documentclass{article}
usepackage{nicematrix}
usepackage{tikz}

ExplSyntaxOn
NewDocumentCommand { Cross } { }
  {
    tl_gput_right:Nx g_nicematrix_code_after_tl
      { __pantigny_cross:n { int_use:c { c@iRow } } }
  }
cs_new_protected:Nn __pantigny_cross:n 
  {
    tikz 
    draw [red, shorten~> = -3pt, shorten~< = -3pt] 
      ( #1-1-medium .west ) -- ( #1- int_use:c { c@jCol } - medium .east ) ;
  }
ExplSyntaxOff

begin{document}

begin{NiceTabular}{cccc}[create-medium-nodes]
$p$ & $q$ & $r$ & $f$ 
hline
0 & 0 & 0 & 0 Cross 
0 & 0 & 1 & 1 
0 & 1 & 0 & 1 
0 & 1 & 1 & 0 Cross 
1 & 0 & 0 & 0 
1 & 1 & 0 & 0 
1 & 1 & 1 & 1 
end{NiceTabular}

end{document}

The use of the medium nodes ensures horizontal lines.

We have to compile several times (because nicematrix uses PGF/Tikz nodes).

Output of the above code

Answered by F. Pantigny on June 7, 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