TransWikia.com

Rotate column titles AND lines in tabular

TeX - LaTeX Asked on February 19, 2021

I would like to not only rotate the text in the header of a tabular, but also insert and rotate a vertical lines by 45 degrees (between CaptionR3 and CaptionR4).

Thanks for your help!

documentclass{article}

usepackage{tabularx}
newcolumntype{x}[1]{!{centeringarraybackslashvrule width #1}}
newcolumntype{L}[1]{>{raggedrightarraybackslash}p{#1}} % linksbündig mit Breitenangabe

usepackage{adjustbox}
usepackage{array}

newcolumntype{R}[2]{%
    >{adjustbox{angle=#1,lap=width-(#2)}bgroup}%
    l%
    <{egroup}%
}
newcommand*rot{multicolumn{1}{R{45}{1em}}}% no optional argument here, please!

usepackage{arydshln}

begin{document}

begin{tabular}{L{0.9cm}|L{2.0cm}|l|l}
multicolumn{1}{c|}{Caption1} & multicolumn{1}{c|}{Caption2} & rot{CaptionR3} & rot{CaptionR4} 
hline      Text1 & Text4 & - 
hdashline  Text2 & Text5 & - 
hdashline  Text3 & Text6 & - 

end{tabular}

end{document}

2 Answers

There is an easy peasy way to do it, but you have to adapt the lenght of the dashed line by hand:

documentclass{article}

usepackage{tabularx}
newcolumntype{x}[1]{!{centeringarraybackslashvrule width #1}}
newcolumntype{L}[1]{>{raggedrightarraybackslash}p{#1}} % linksbündig mit Breitenangabe

usepackage{adjustbox}
usepackage{array}

newcolumntype{R}[2]{%
    >{adjustbox{angle=#1,lap=width-(#2)}bgroup}%
    l%
    <{egroup}%
}
newcommand*rot{multicolumn{1}{R{45}{1em}}}% no optional argument here, please!

usepackage{arydshln}

begin{document}

begin{tabular}{L{0.9cm}|L{2.0cm}|l|@{}l@{}l}
  multicolumn{1}{c|}{Caption1} & multicolumn{1}{c|}{Caption2} & rot{CaptionR3} &
  hspace*{-1ex}rot{hspace*{-1ex}- - - - - - - - -} &rot{CaptionR4} 
hline      Text1 & Text4 & - 
hdashline  Text2 & Text5 & - 
hdashline  Text3 & Text6 & - 

end{tabular}

end{document}

tabular with rotated, dashed line

Answered by Keks Dose on February 19, 2021

Here is a solution with {NiceTabular} of nicematrix.

The angle of the rotation can be set by the parameter angle.

A command Rot is defined to rotate the labels (that command does a translation rightwards in order to have a better output).

A command MixedRule must be used in the CodAfter of {NiceTabular} in order to draw the rule corresponding to that label (no symbol | should be put in the preamble for those rules).

You need several compilations.

documentclass{article}
usepackage{xparse}
usepackage{nicematrix}
usepackage{tikz}
usetikzlibrary{calc}

ExplSyntaxOn
NewExpandableDocumentCommand { ValuePlusOne } { m } 
  { int_eval:n { int_use:c { c @ #1 } + 1 } }
NewExpandableDocumentCommand { Sec } { m } 
  { fp_eval:n { secd ( #1 ) } }
NewDocumentCommand { Rot } { m }
  { 
    hbox_to_wd:nn { 1 em }
      { 
        hbox_overlap_right:n 
          { 
            skip_horizontal:n { fp_to_dim:n { 7 * cosd (Angle) } } 
            rotatebox{Angle}{#1}
          } 
      } 
  }
ExplSyntaxOff

NewDocumentCommand { MixedRule } { m }
  {
    begin{tikzpicture}
    coordinate (a) at (row-2-|col-#1) ;
    coordinate (b) at (row-1-|col-#1) ;
    draw (a) -- ($(a)!Sec{90-Angle}!Angle-90:(b)$) ;
    %
    draw (row-2-|col-#1) -- (row-ValuePlusOne{iRow}-|col-#1) ;
    end{tikzpicture}
  }

begin{document}

defAngle{45}

begin{NiceTabular}{c|c|ccc}
Caption 1 & Caption 2 & Rot{Caption 3} & Rot{Caption 4} & Rot{Caption 5} hline
Text1 & Text4 & -- & -- & --  hline  
Text2 & Text5 & -- & -- & --  hline  
Text3 & Text6 & -- & -- & --
CodeAfter
MixedRule{4}
MixedRule{5}
end{NiceTabular}
    
bigskip
defAngle{60}

begin{NiceTabular}{c|c|ccc}
Caption 1 & Caption 2 & Rot{Cap 3} & Rot{Cap 4} & Rot{Cap 5} hline
Text1 & Text4 & -- & -- & --  hline  
Text2 & Text5 & -- & Text7 & --  hline  
Text3 & Text6 & -- & -- & --
CodeAfter
MixedRule{4}
MixedRule{5}
end{NiceTabular}
    
bigskip
defAngle{90}

begin{NiceTabular}{c|c|ccc}
Caption 1 & Caption 2 & Rot{Caption 3} & Rot{Caption 4} & Rot{Caption 5} hline
Text1 & Text4 & -- & -- & --  hline  
Text2 & Text5 & -- & Text7 & --  hline  
Text3 & Text6 & -- & -- & --
CodeAfter
MixedRule{4}
MixedRule{5}
end{NiceTabular}

end{document}

Ouput of the above code

The last example is with a value of 90 for the parameter Angle.

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