6

I am trying to do a table. using tikz matrix, like the one below using Tikz. The problem is that I don't know how to do the first row, with "clutch" "brake" neither do I know how to do the last column. I am grateful for any pointers.

\documentclass[border=3mm,tikz]{standalone}


\usetikzlibrary{matrix,calc,positioning}
\tikzset{ 
common/.style={
  text depth=1.25ex,
  text height=2.5ex,
  inner sep=0pt
  },
table/.style={
  matrix of math nodes,
  common,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=3em,align=center},
  nodes in empty cells,
  }
}

\begin{document}

\begin{tikzpicture}[blue]
      
\matrix at (11,-3.5) (mat1) [table]
{
  G & A & B & E & C & D  & i \\
  1 & \bullet &   &   &   & \bullet & 4.17 \\
  2 & \bullet &   &   & \bullet &  & 2.34\\
  3 &  \bullet  &  \bullet  &   &   &   & 1.52\\
  4 &  \bullet  &   &  \bullet  &   &  &  1.14\\
  5 &    & \bullet  &  \bullet  &   &  &  0.87\\
  6 &    &   &  \bullet  & \bullet  & &   0.69\\
  R &    & \bullet  &    &   &  \bullet & -3.40\\
};

\end{tikzpicture}


\end{document}


What I managed to do:

enter image description here

What I want to do:

enter image description here

2
  • What is exactly the purpose of using TikZ when a simple table would do the trick?
    – SebGlav
    Commented Dec 5, 2022 at 13:32
  • It is because I want to place the Tikz matrix beside a picture that is also done using Tikz. Commented Dec 5, 2022 at 14:46

2 Answers 2

8

Here's an option using nicematrix and TikZ:

enter image description here

\documentclass{article}

\usepackage{nicematrix, tikz}

\begin{document}

\begin{NiceTabular}{cccccccc}[hvlines]
  \Block{2-1}{}G & \Block{1-3}{Clutch} &&& \Block{1-2}{Break} && \Block{2-1}{}$i$ & \Block{9-1}{}Gear\\
  & A & B & E & C & D  &  & step\\
  1 & \textbullet &   &   &   & \textbullet & 4.17 & \\
  2 & \textbullet &   &   & \textbullet &  & 2.34 &\\
  3 &  \textbullet  &  \textbullet  &   &   &   & 1.52 &\\
  4 &  \textbullet  &   &  \textbullet  &   &  &  1.14 &\\
  5 &    & \textbullet  &  \textbullet  &   &  &  0.87 &\\
  6 &    &   &  \textbullet  & \textbullet  & &   0.69 &\Block{2-1}{{}\\[-1ex]Total\\[-.5ex]6.04}\\
  R &    & \textbullet  &    &   &  \textbullet & $-3.40$ & \\
  \CodeAfter
    \tikz{
      \foreach \h/\k[evaluate=\h as \l using \h+1] in {3/1.78,4/1.54,5/1.33,6/1.31,7/1.26,8/}
      {\draw (\h.5-|8) -- (\h.5-|9); \node at (\l-|8.5){\k};}
    }
\end{NiceTabular}

\end{document}
0
4

It's not very pretty but I didn't even try. There's a lot of manual stuff that should be made into a style.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix, ext.positioning-plus}
\tikzset{% https://tex.stackexchange.com/a/660100/16595
  columns/.style 2 args={/utils/tempa/.style={column ##1/.append style={#2}},/utils/tempa/.list={#1}},columns*/.style 2 args={columns={#1}{nodes={#2}}},
  rows/.style 2 args={/utils/tempa/.style={row ##1/.append style={#2}},/utils/tempa/.list={#1}},rows*/.style 2 args={rows={#1}{nodes={#2}}}}
\tikzset{ 
  common/.style={
    text depth=1.25ex, text height=2.5ex,     
    inner ysep=+0pt, draw=black,
    align=center,
    text width=width("Brake")-4*(\pgfkeysvalueof{/pgf/inner xsep}),
  },
  common'/.style={
    common, align=right, text width=width("$-0.00$")},
  table/.style={
    nodes=common, matrix of math nodes,
    every outer matrix/.append style={inner sep=+0pt},
    row sep=-\pgflinewidth, column sep=-\pgflinewidth, nodes in empty cells},
}
\begin{document}
\begin{tikzpicture}[blue]
\matrix at (11,-3.5) (mat1) [
  table,
  columns*={7}{common'},
  rows*={8}{inner ysep=.3333em}
]{
  \path; & A       & B       & E       & C       & D  & \path; \\
  1 & \bullet &         &         &         & \bullet &  4.17 \\
  2 & \bullet &         &         & \bullet &         &  2.34 \\
  3 & \bullet & \bullet &         &         &         &  1.52 \\
  4 & \bullet &         & \bullet &         &         &  1.14 \\
  5 &         & \bullet & \bullet &         &         &  0.87 \\
  6 &         &         & \bullet & \bullet &         &  0.69 \\
  R &         & \bullet &         &         & \bullet & -3.40 \\
};
\path[
  node distance=-\pgflinewidth,
  l/.style={l'={$##1$}},
  l'/.style={label={[anchor=north, align=center]north:{##1}}},
  common''/.style={common', text width=width("Total")}]
  {[common/.append style={text width=}] % suppress overfull hboxes
    node[common, above=of -(mat1-1-2)(mat1-1-4)] (mat1-0) {Clutch}
    node[common, above=of -(mat1-1-5)(mat1-1-6)] {Brake}
  }
  node[right=of |(mat1-0)(mat1-1-6), common', l=i] (mat1-i) {}
  node[left=of |(mat1-0)(mat1-1-2), common, l=G] (mat1-G) {}
  foreach \Val [count=\vAl from 2, count=\vaL from 3] in {1.78, 1.54, 1.33, 1.31, 1.26}{
    node[
      common'',
      right=of (mat1-\vAl-7)(mat1-\vaL-7),
    ] (mat1-\vaL-8) {\Val}
  }
  node[
    common'',
    text height=, text depth=,
    right={of |([yshift=\pgflinewidth]mat1-7-8.south west)(mat1-8-7)}
  ] {Total\\6.04}
  node[
    common'',
    text height=, text depth=,
    align=center,
    right={of |([yshift=-\pgflinewidth]mat1-3-8.north west)(mat1-i)},
    l'={Gear\\step},
  ] {}
;
\end{tikzpicture}
\end{document}

Output

enter image description here

1
  • It's also possible to shift down an added last column but the special headers will need work still. Commented Dec 5, 2022 at 17:55

You must log in to answer this question.

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