使用 Tikz 制作带有偏移列的表格

使用 Tikz 制作带有偏移列的表格

我正在尝试制作一个表格。使用 tikz 矩阵,就像下面使用 Tikz 制作的表格一样。问题是我不知道如何制作第一行,使用“离合器”“刹车”,也不知道如何制作最后一列。我很感激任何指点。

\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}


我成功做到了:

在此处输入图片描述

我想做的事:

在此处输入图片描述

答案1

这是一个使用nicematrixTi 的选项Z:

在此处输入图片描述

\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}

答案2

它不是很漂亮,但我甚至没有尝试过。有很多手工的东西应该做成一种风格。

代码

\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}

输出

在此处输入图片描述

相关内容