将表格行调整为 tikz 图形

将表格行调整为 tikz 图形

我有一个 tikz 图形,其中表格嵌入在图形中绘制的水平线内。我需要根据图形位置调整表格的行。我尝试手动调整表格间距,但结果不一致。只有最后一行以某种方式对齐。第一个条目 (y, y_n) 偏离了一行。任何有关如何实现此目的的帮助都将不胜感激。

在此处输入图片描述

平均能量损失

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{booktabs}

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\draw [ultra thick] (-6.,2.)-- (-6.,1.);
\draw [ultra thick] (-6.,2.)-- (-4.,2.);
\draw [ultra thick] (-4.,1.)-- (-4.,4.);
\draw [ultra thick] (0.,7.)-- (0.,1.);
\draw [ultra thick] (-2.,3.)-- (-2.,1.);
\draw [ultra thick] (3.,5.)-- (3.,1.);
\draw [ultra thick] (5.,3.)-- (5.,1.);
\draw [ultra thick] (1.,2.)-- (0.,2.);
\draw [ultra thick] (3.,5.)-- (0.,5.);
\draw [ultra thick] (-4.,4.)-- (0.,4.);
\draw [ultra thick] (-4.,1.)-- (-5.,1.);
\draw [ultra thick] (-2.,1.)-- (-3.,1.);
\draw [ultra thick] (-2.,3.)-- (0.,3.);
\draw [ultra thick] (0.,1.)-- (-1.,1.);
\draw [ultra thick] (1.,2.)-- (1.,1.);
\draw [ultra thick] (3.,1.)-- (2.,1.);
\draw [ultra thick] (3.,2.)-- (4.,2.);
\draw [ultra thick] (4.,2.)-- (4.,1.);
\draw [ultra thick] (5.,1.)-- (6.,1.);
\draw [ultra thick] (5.,3.)-- (3.,3.);
\draw [gray] (-6.5,0.5)-- (9,0.5);
\draw [gray] (-6.5,1.5)-- (9,1.5);
\draw [gray] (-6.5,2.5)-- (9,2.5);
\draw [gray] (-6.5,3.5)-- (9,3.5);
\draw [gray] (-6.5,4.5)-- (9,4.5);
\draw [gray] (-6.5,5.5)-- (9,5.5);
\draw [gray] (-6.5,6.5)-- (9,6.5);
\draw [gray] (-6.5,7.5)-- (9,7.5);
\draw [gray] (-6.5,8.5)-- (9,8.5);


\draw node (-3,-.25) {Symbol \tikz \fill [] circle (0.25cm); denotes one pair of rabbits.};
\fill [] (0.,7.) circle (0.25cm);
\fill [] (0.,6.) circle (0.25cm);
\fill [] (0.,5.) circle (0.25cm);
\fill [] (0.,4.) circle (0.25cm);
\fill [] (0.,3.) circle (0.25cm);
\fill [] (0.,2.) circle (0.25cm);
\fill [] (0.,1.) circle (0.25cm);
\fill [] (-1.,1.) circle (0.25cm);
\fill [] (-2.,1.) circle (0.25cm);
\fill [] (-3.,1.) circle (0.25cm);
\fill [] (-4.,1.) circle (0.25cm);
\fill [] (-5.,1.) circle (0.25cm);
\fill [] (-6.,1.) circle (0.25cm);
\fill [] (-6.,2.) circle (0.25cm);
\fill [] (-4.,2.) circle (0.25cm);
\fill [] (-4.,3.) circle (0.25cm);
\fill [] (-4.,4.) circle (0.25cm);
\fill [] (-2.,3.) circle (0.25cm);
\fill [] (-2.,2.) circle (0.25cm);
\fill [] (1.,2.) circle (0.25cm);
\fill [] (1.,1.) circle (0.25cm);
\fill [] (2.,1.) circle (0.25cm);
\fill [] (3.,1.) circle (0.25cm);
\fill [] (3.,2.) circle (0.25cm);
\fill [] (3.,3.) circle (0.25cm);
\fill [] (3.,4.) circle (0.25cm);
\fill [] (3.,5.) circle (0.25cm);
\fill [] (4.,2.) circle (0.25cm);
\fill [] (4.,1.) circle (0.25cm);
\fill [] (5.,1.) circle (0.25cm);
\fill [] (5.,2.) circle (0.25cm);
\fill [] (5.,3.) circle (0.25cm);
\fill [] (6.,1.) circle (0.25cm);

\node (tbl) at (9,4.5){
\begin{tabularx}{.2\textwidth}{cc}
$n $ & $y_{n}$ \\[20pt]
1 & 1 \\[20pt]
2 & 1 \\[20pt]
3 & 2 \\[20pt]
4 & 3 \\[20pt]
5 & 5 \\[20pt]
6 & 8 \\[20pt]
7 & 13 \\[20pt]
\end{tabularx}};

\end{tikzpicture}
\end{document}

答案1

这比你要求的要多,但只是为了演示一种完全不同的绘制此类图表的方法。像 AboAmmar 一样,我使用循环将“表”添加为节点。

在此处输入图片描述

\documentclass[10pt]{article}
\usepackage{tikz}
\pagestyle{empty}

\begin{document}
\begin{tikzpicture}[
   scale=0.75,
   yscale=-1
  ]

% draw horizontal lines with labels  
\foreach \nn/\yn [count=\i from 0] in {n/y_n,1/1,2/1,3/2,4/3,5/5,6/8,7/13}
  {
    \draw [gray] (0, \i+0.5) -- +(15,0);
    \node at (14.7,\i) {$\yn$};
    \node at (14,\i) {$\nn$};
    
    \ifnum \i=0
        \draw [gray] (0, \i-0.5) -- +(15,0);
    \fi
  }

% draw connections
\draw [ultra thick]
      (1,7) |- (3,6) |- (2,7)
      (3,6) |- (7,4) |- (5,5) |- (4,7)
      (7,5) |- (6,7)
      (7,6) -| (8,7)
      (7,4) -- (7,1)
      (7,3) -| (10,7) -- (9,7)
      (10,6) -| (11,7)
      (10,5) -| (12,7) -- (13,7);

% draw markers
\draw [draw=none,mark=*,mark size=2mm]
   plot coordinates {%
      (7,1)
      (7,2)
      (7,3)(10,3)
      (3,4)(7,4)(10,4)
      (3,5)(5,5)(7,5)(10,5)(12,5)
      (1,6)(3,6)(5,6)(7,6)(8,6)(10,6)(11,6)(12,6)
      (1,7)(2,7)(3,7)(4,7)(5,7)(6,7)(7,7)(8,7)(9,7)(10,7)(11,7)(12,7)(13,7)
      };

\end{tikzpicture}
\end{document}

答案2

删除表格,它不会与 tikz 图片很好地融合。改用以下两行:

\foreach \y[count=\i] in {7,6,...,1} \node at (8,\y){\i};
\foreach \y[count=\i] in {13,8,5,3,2,1,1} \node at (8.9,\i){\y};

在点的右边添加数字。

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{booktabs}

\pagestyle{empty}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
\draw [ultra thick] (-6.,2.)-- (-6.,1.);
\draw [ultra thick] (-6.,2.)-- (-4.,2.);
\draw [ultra thick] (-4.,1.)-- (-4.,4.);
\draw [ultra thick] (0.,7.)-- (0.,1.);
\draw [ultra thick] (-2.,3.)-- (-2.,1.);
\draw [ultra thick] (3.,5.)-- (3.,1.);
\draw [ultra thick] (5.,3.)-- (5.,1.);
\draw [ultra thick] (1.,2.)-- (0.,2.);
\draw [ultra thick] (3.,5.)-- (0.,5.);
\draw [ultra thick] (-4.,4.)-- (0.,4.);
\draw [ultra thick] (-4.,1.)-- (-5.,1.);
\draw [ultra thick] (-2.,1.)-- (-3.,1.);
\draw [ultra thick] (-2.,3.)-- (0.,3.);
\draw [ultra thick] (0.,1.)-- (-1.,1.);
\draw [ultra thick] (1.,2.)-- (1.,1.);
\draw [ultra thick] (3.,1.)-- (2.,1.);
\draw [ultra thick] (3.,2.)-- (4.,2.);
\draw [ultra thick] (4.,2.)-- (4.,1.);
\draw [ultra thick] (5.,1.)-- (6.,1.);
\draw [ultra thick] (5.,3.)-- (3.,3.);
\draw [gray] (-6.5,0.5)-- (9,0.5);
\draw [gray] (-6.5,1.5)-- (9,1.5);
\draw [gray] (-6.5,2.5)-- (9,2.5);
\draw [gray] (-6.5,3.5)-- (9,3.5);
\draw [gray] (-6.5,4.5)-- (9,4.5);
\draw [gray] (-6.5,5.5)-- (9,5.5);
\draw [gray] (-6.5,6.5)-- (9,6.5);
\draw [gray] (-6.5,7.5)-- (9,7.5);
\draw [gray] (-6.5,8.5)-- (9,8.5);

\foreach \y[count=\i] in {7,6,...,1} \node at (8,\y){\i};
\foreach \y[count=\i] in {13,8,5,3,2,1,1} \node at (8.9,\i){\y};

\draw node (-3,-.25) {Symbol \tikz \fill [] circle (0.25cm); denotes one pair of rabbits.};
\fill [] (0.,7.) circle (0.25cm);
\fill [] (0.,6.) circle (0.25cm);
\fill [] (0.,5.) circle (0.25cm);
\fill [] (0.,4.) circle (0.25cm);
\fill [] (0.,3.) circle (0.25cm);
\fill [] (0.,2.) circle (0.25cm);
\fill [] (0.,1.) circle (0.25cm);
\fill [] (-1.,1.) circle (0.25cm);
\fill [] (-2.,1.) circle (0.25cm);
\fill [] (-3.,1.) circle (0.25cm);
\fill [] (-4.,1.) circle (0.25cm);
\fill [] (-5.,1.) circle (0.25cm);
\fill [] (-6.,1.) circle (0.25cm);
\fill [] (-6.,2.) circle (0.25cm);
\fill [] (-4.,2.) circle (0.25cm);
\fill [] (-4.,3.) circle (0.25cm);
\fill [] (-4.,4.) circle (0.25cm);
\fill [] (-2.,3.) circle (0.25cm);
\fill [] (-2.,2.) circle (0.25cm);
\fill [] (1.,2.) circle (0.25cm);
\fill [] (1.,1.) circle (0.25cm);
\fill [] (2.,1.) circle (0.25cm);
\fill [] (3.,1.) circle (0.25cm);
\fill [] (3.,2.) circle (0.25cm);
\fill [] (3.,3.) circle (0.25cm);
\fill [] (3.,4.) circle (0.25cm);
\fill [] (3.,5.) circle (0.25cm);
\fill [] (4.,2.) circle (0.25cm);
\fill [] (4.,1.) circle (0.25cm);
\fill [] (5.,1.) circle (0.25cm);
\fill [] (5.,2.) circle (0.25cm);
\fill [] (5.,3.) circle (0.25cm);
\fill [] (6.,1.) circle (0.25cm);

%\node (tbl) at (9,4.5){
%\begin{tabularx}{.2\textwidth}{cc}
%$n $ & $y_{n}$ \\[20pt]
%1 & 1 \\[20pt]
%2 & 1 \\[20pt]
%3 & 2 \\[20pt]
%4 & 3 \\[20pt]
%5 & 5 \\[20pt]
%6 & 8 \\[20pt]
%7 & 13 \\[20pt]
%\end{tabularx}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

如果您想使用表格来执行此操作,这里有一个{NiceArray}带有 的解决方案nicematrix

\documentclass[10pt]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\pagestyle{empty}

\def \N { \raise-1mm\hbox{\scalebox{2}{$\bullet$}} }
\def \C { \Cdots }
\def \V { \Vdots }

\NiceMatrixOptions
  { 
    rules/width=0.2pt ,
    xdots = 
      { 
        line-style={solid,thick},
        shorten=-0.5mm
      } 
  }

\begin{document}

$\begin{NiceArray}{*{27}{c}}[nullify-dots]
  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &  &n&y_n \\ \hline \\
  &  &  &  &  &  &  &  &  &  &  &  &\N&  &  &  &  &  &  &  &  &  &  &  &  &1&1 \\
  &  &  &  &  &  &  &  &  &  &  &  &\V&  &  &  &  &  &  &  &  &  &  &  &  & & \\ \hline \\
  &  &  &  &  &  &  &  &  &  &  &  &\N&  &  &  &  &  &  &  &  &  &  &  &  &2&1 \\
  &  &  &  &  &  &  &  &  &  &  &  &\V&  &  &  &  &  &  &  &  &  &  &  &  & & \\ \hline \\
  &  &  &  &  &  &  &  &  &  &  &  &\N&  &  &\C&  &  &\N&  &  &  &  &  &  &3&2 \\
  &  &  &  &  &  &  &  &  &  &  &  &\V&  &  &  &  &  &\V&  &  &  &  &  &  & & \\ \hline \\
  &  &  &  &\N&  &  &  &\C&  &  &  &\N&  &  &  &  &  &\N&  &  &  &  &  &  &4&3\\
  &  &  &  &\V&  &  &  &  &  &  &  &\V&  &  &  &  &  &\V&  &  &  &  &  &  & & \\ \hline \\
  &  &  &  &\N&  &  &  &\N&  &\C&  &\N&  &  &  &  &  &\N&  &\C&  &\N&  &  &5&5\\
  &  &  &  &\V&  &  &  &\V&  &  &  &\V&  &  &  &  &  &\V&  &  &  &\V&  &  & & \\ \hline \\
\N&  &\C&  &\N&  &  &  &\N&  &  &  &\N&\C&\N&  &  &  &\N&\C&\N&  &\N&  &  &6&8 \\
\V&  &  &  &\V&  &  &  &\V&  &  &  &\V&  &\V&  &  &  &\V&  &\V&  &\V&  &  & &  \\ \hline \\
\N&  &\N&\C&\N&  &\N&\C&\N&  &\N&\C&\N&  &\N&  &\N&\C&\N&  &\N&  &\N&\C&\N&7&13\\ 
\end{NiceArray}$

\end{document}

上述代码的输出

相关内容