TransWikia.com

Diagonal lines in table cell

TeX - LaTeX Asked by LiN on June 22, 2021

I need this table:

table

(each row and column should have the same height and length, last cell should be divided by diagonal line). I tried the following code:

documentclass[11pt]{article}
usepackage[T1]{fontenc}
usepackage{array}
usepackage{makecell}
newcolumntype{x}[1]{>{centeringletnewlinearraybackslashhspace{0pt}}p{#1}}
begin{document}
setlength{extrarowheight}{0.1cm}
begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}hline
&&&&20 hline
&&&&30 hline
&&&&45 hline
15&12&18&50&diaghead(-3,2){hskip hsize}{$a_i$}{$b_j$} hline
end{tabular}
end{document}

but text in the last cell is displayed incorrectly and cells are not the same.

table - latex

How can I change that?

6 Answers

Here's a solution using TikZ:

documentclass[11pt]{article}
usepackage[T1]{fontenc}
usepackage{tikz}
usetikzlibrary{matrix}

begin{document}

begin{tikzpicture}%[thick]
  matrix (mat) [%
    matrix of nodes,
    nodes in empty cells,
    text width=0.8cm,
    text height=10pt,
    text depth=2pt,
    text badly centered
  ] 
  {%
     & & & & 20 
     & & & & 30  
     & & & & 45  
    15 & 12 & 18 & 50 & raisebox{5pt}{$a_i$}hspace{-15pt}llap{raisebox{-1pt}{$b_j$}}
  };
  % horizontal lines
  foreach i in {1,2,3,4}
        draw (mat-i-1.north west) -- (mat-i-5.north east);
  draw (mat-4-1.south west) -- (mat-4-5.south east);
  % vertical lines
  foreach j in {1,2,3,4,5}
    draw (mat-1-j.north west) -- (mat-4-j.south west);
  draw (mat-1-5.north east) -- (mat-4-5.south east);
  % diagonal line    
  draw (mat-4-5.north west) -- (mat-4-5.south east);
end{tikzpicture}

end{document}

EDIT: in the solution I initially gave, the last cell contents was not what the OP wanted. I corrected it.

Answered by Gonzalo Medina on June 22, 2021

It might be possible to draw a diagonal line which fits exactly in a table cell, but it might be easier to draw the whole table as a picture.

Here my attempt using tikz. For large tables the need for nodes for each cell might be quite an effort, but it should be OK for smaller ones.

documentclass[11pt]{article}
usepackage[T1]{fontenc}
usepackage{tikz}
begin{document}
begin{tikzpicture}[x=.75cm,y=.5cm]
    draw (0,0) grid [step=1] (5,4);
    node at (0.5,0.5) {15};
    node at (1.5,0.5) {12};
    node at (2.5,0.5) {18};
    node at (3.5,0.5) {50};
    node at (4.5,3.5) {20};
    node at (4.5,2.5) {30};
    node at (4.5,1.5) {45};
    draw (4,1) -- (5,0);
    node at (5.0,1.0) [below left,inner sep=1pt] {small$a_i$};
    node at (4.0,0.0) [above right,inner sep=1pt] {small$b_j$};
end{tikzpicture}
end{document}

Result

Answered by Martin Scharrer on June 22, 2021

Exact solution with TikZ:

documentclass[11pt]{article}
usepackage[T1]{fontenc}
usepackage{array}
usepackage{makecell}
newcolumntype{x}[1]{>{centeringarraybackslash}p{#1}}

usepackage{tikz}
newcommanddiag[4]{%
  multicolumn{1}{p{#2}|}{hskip-tabcolsep
  $vcenter{begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#1]
  path[use as bounding box] (0,0) rectangle (#2+2tabcolsep,baselineskip);
  node[minimum width={#2+2tabcolsep},minimum height=baselineskip+extrarowheight] (box) {};
  draw (box.north west) -- (box.south east);
  node[anchor=south west] at (box.south west) {#3};
  node[anchor=north east] at (box.north east) {#4};
 end{tikzpicture}}$hskip-tabcolsep}}

begin{document}
setlength{extrarowheight}{0.1cm}
begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}hline
&&&&20 hline
&&&&30 hline
&&&&45 hline
15&12&18&50&diag{.1em}{.5cm}{$a_i$}{$b_j$} hline
end{tabular}
end{document}

enter image description here

Also it is a reimplementation of diaghead.

Answered by Leo Liu on June 22, 2021

Here's another solution using Tikz and based on a given solution by Heiko Oberdiek.

documentclass{article}
pagestyle{empty}% for cropping
usepackage{array}
usepackage{makecell}
newcolumntype{x}[1]{>{centeringarraybackslash}p{#1}}

usepackage{tikz}
usetikzlibrary{calc}
usepackage{zref-savepos}

newcounter{DiagonalizedEntry}
renewcommand*{theDiagonalizedEntry}{NTE-thevalue{DiagonalizedEntry}}

newcommand*{diagonalize}[2]{%
  multicolumn{1}{@{}c@{}|}{%
    stepcounter{DiagonalizedEntry}%
    vadjust pre{zsavepos{theDiagonalizedEntry t}}% top
    vadjust{zsavepos{theDiagonalizedEntry b}}% bottom
    zsavepos{theDiagonalizedEntry l}% left
    hspace{0pt plus 1filll}%
    zsavepos{theDiagonalizedEntry r}% right
    tikz[overlay]{%
      draw[red]
        let
          n{llx}={zposx{theDiagonalizedEntry l}sp-zposx{theDiagonalizedEntry r}sp}, % x left
          n{urx}={0}, % x right
          n{lly}={zposy{theDiagonalizedEntry b}sp-zposy{theDiagonalizedEntry r}sp}, % y bottom
          n{ury}={zposy{theDiagonalizedEntry t}sp-zposy{theDiagonalizedEntry r}sp} %  y top
    in
        (n{llx}, n{ury}) -- (n{urx}, n{lly})
    node[anchor=south west] at (n{llx}, n{lly}) {#1}
    node[anchor=north east] at (n{urx}, n{ury}) {#2}
    ;
    }% 
  }%
}

begin{document}
renewcommand{arraystretch}{2}
renewcommandtabcolsep{10pt}
begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}hline
&&&&20 hline
&&&&30 hline
&&&&45 hline
15&12&18&50&diagonalize{$a_i$}{$b_j$}  hline
end{tabular}
end{document}

enter image description here

Answered by Hamed on June 22, 2021

Okay as this seems to be the question that all duplicates link to eventually, I want to include @Leo Liu's solution from here as well as this seems to solve the problem in the easiest way.

In short: Use the @Leo Liu's package diagbox.

MWE (coming from a different context and does not exactly answer to this question) shamelessly copied from Leo's answer (link above):

documentclass{article}

usepackage{diagbox}

begin{document}

begin{tabular}{|l|c|c|}hline
diagbox[width=10em]{DiagColumn Head I}{Diag ColumnHead II}&
  Second & Third  hline
& foo & bar  hline
end{tabular}

end{document}

Resulting in
Compiled result of MWE

If you like this solution make sure you go ahead and show Leo's original answer some love.


Admittedly this solution doesn't work so well (out of the box) with the provided MWE but if one uses normal column types instead of the custom ones, one gets a really good result:

documentclass{standalone}
usepackage{diagbox}

begin{document}
    begin{tabular}{|c|c|c|c|c|}
        hline
           &    &    &    &           20            hline
           &    &    &    &           30            hline
           &    &    &    &           45            hline
        15 & 12 & 18 & 50 & diagbox{$a_i$}{$b_j$}  hline
    end{tabular}
end{document}

Results in
Compiled result of the MWE2

Answered by Raven on June 22, 2021

A solution with {NiceTabular} of nicematrix which has a built-in command diagbox. Moreover, you can draw all the rules with only one key hvlines.

documentclass{article}
usepackage{nicematrix}

begin{document}
setlength{extrarowheight}{1mm}
begin{NiceTabular}{ccccc}[hvlines]
   &    &    &    &           20            
   &    &    &    &           30            
   &    &    &    &           45            
15 & 12 & 18 & 50 & diagbox{$a_i$}{$b_j$}  
end{NiceTabular}
end{document}

Remark: For versions of nicematrix prior to 5.0, you have to put {CCCCC} instead of {ccccc}.

Ouput of the above code

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