使用 Tikz 在 LaTeX 中制作表格

使用 Tikz 在 LaTeX 中制作表格

我尝试用Tikz两种不同的方法制作一个相当复杂的表格,但都没有得到完美的结果。在我附上的例子中,你可以看到这两种方法的结果。我不明白为什么在第一种情况下较小的方框不会关闭,而在第二种情况下,为什么行与行之间会有间隙。

如有任何建议我将不胜感激。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\usepackage{adjustbox}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
%\usepackage[latin1]{inputenc}



\begin{document}

\begin{center}
    \begin{adjustbox}{max totalsize={.8\textwidth}{.8\textheight},center}   
\begin{tikzpicture}[
%row 1/.style={nodes={fill=yellow}},
%column 4/.style={nodes={fill=pink}},
%row 4 column 5/.style={nodes={fill=blue}}
]

\matrix(m) [matrix of nodes,nodes in empty cells,nodes={minimum height=1cm,minimum width=1cm,outer sep=0pt}]
{
      &       &      &    &     &   &   &   &   &  &  &  &  &\\
       &    10 &      &     30 &      4 &    &  50&      8 &      &   &    12&   &   &  \\
       &   &   &   &   &   & &   &   &  &  &  &  & 125  \\
      &   &    &  10  &   &   & 30 &   &   & &  &  &  & \\
       & 4  &   &  40 & -4  &   &  &  -4 &   &  35 & 10 &  &  & \\
      &   &  &  &      &  &  &   &   &  &  &  &  & 55 \\
       &    &    &  10 &   &   & &   &   & 5 &  &  &  &  \\
       & 6  &   &    & 4  &   & &  8 &   &  &  -8 &  & 20 &  \\
       &   &    &   &   &    &  &   &   &  &  &  &  & 30 \\
       &   &   &   &   &   &   &   &   &  &  &  &  & \\
       &   & 90  &   &   & $\le 60$  &   &   & $\le 65$  &  &  & 20 &  & \\
};


\draw (m-2-2.north west) rectangle (m-10-13.south east);

% Lineas horizontales
\draw (m-4-2.south west) -- (m-4-13.south east);
\draw (m-7-2.south west) -- (m-7-13.south east);

% Lineas verticales
\draw (m-2-4.north east) -- (m-10-4.south east);
\draw (m-2-7.north east) -- (m-10-7.south east);
\draw (m-2-10.north east) -- (m-10-10.south east);





\draw (m-4-4.north west) rectangle (m-4-4.south east);

\draw (m-2-4.south west) -- (m-2-4.south east);
\draw (m-2-3.north east) -- (m-2-3.south east);
%limite inferior
\draw (m-3-4.south west) -- (m-3-4.south east);
\draw (m-4-3.north east) -- (m-4-3.south east);

%limite superior
\draw (m-2-7.south west) -- (m-2-7.south east);
\draw (m-2-6.north east) -- (m-2-6.south east);
%limite inferior
\draw (m-3-7.south west) -- (m-3-7.south east);
\draw (m-4-6.north east) -- (m-4-6.south east);

% Segunda fila
% Casilla 2-1
%limite superior
\draw (m-5-4.south west) -- (m-5-4.south east);
\draw (m-5-3.north east) -- (m-5-3.south east);
%limite inferior
\draw (m-6-4.south west) -- (m-6-4.south east);
\draw (m-7-3.north east) -- (m-7-3.south east);

% Casilla 2-3
%limite superior
\draw (m-5-10.south west) -- (m-5-10.south east);
\draw (m-5-9.north east) -- (m-5-9.south east);
%limite inferior
\draw (m-6-10.south west) -- (m-6-10.south east);
\draw (m-7-9.north east) -- (m-7-9.south east);

% Tercera fila
% Casilla 3-4
%limite superior
\draw (m-8-13.south west) -- (m-8-13.south east);
\draw (m-8-12.north east) -- (m-8-12.south east);


% Para dibujar cuadrados
%\node[draw=blue,rounded corners = 1ex,fit=(m-2-2)(m-4-4),inner sep = -5pt] {};



%\node[label=above right:$20$] at (m-2-3){33};

\end{tikzpicture}
\end{adjustbox}
\end{center}



\begin{center}
    \begin{adjustbox}{max totalsize={.8\textwidth}{.8\textheight},center}   
\begin{tikzpicture}[
%row 1/.style={nodes={fill=yellow}},
%column 4/.style={nodes={fill=pink}},
%row 4 column 5/.style={nodes={fill=blue}}
]

\matrix(m) [matrix of nodes,nodes in empty cells,nodes={minimum height=1cm,minimum width=1cm,outer sep=0pt}]
{
      &       &      &    &     &   &   &   &   &  &  &  &  &\\
       &    10 &      &     30 &      4 &    &  50&      8 &      &   &    12&   &   &  \\
       &   &   &   &   &   & &   &   &  &  &  &  & 125  \\
      &   &    &  10  &   &   & 30 &   &   & &  &  &  & \\
       & 4  &   &  40 & -4  &   &  &  -4 &   &  35 & 10 &  &  & \\
      &   &  &  &      &  &  &   &   &  &  &  &  & 55 \\
       &    &    &  10 &   &   & &   &   & 5 &  &  &  &  \\
       & 6  &   &    & 4  &   & &  8 &   &  &  -8 &  & 20 &  \\
       &   &    &   &   &    &  &   &   &  &  &  &  & 30 \\
       &   &   &   &   &   &   &   &   &  &  &  &  & \\
       &   & 90  &   &   & $\le 60$  &   &   & $\le 65$  &  &  & 20 &  & \\
};


\draw (m-2-2.north west) rectangle (m-10-13.south east);

% Lineas horizontales
\draw (m-4-2.south west) -- (m-4-13.south east);
\draw (m-7-2.south west) -- (m-7-13.south east);

% Lineas verticales
\draw (m-2-4.north east) -- (m-10-4.south east);
\draw (m-2-7.north east) -- (m-10-7.south east);
\draw (m-2-10.north east) -- (m-10-10.south east);

% Primera fila
%limite superior
\node[draw=blue,fit=(m-2-4)(m-2-4),inner sep = 0pt] {};
\node[draw=blue,fit=(m-4-4)(m-4-4),inner sep = 0pt] {};
\node[draw=blue,fit=(m-2-2)(m-4-4),inner sep = 0pt] {};


%limite superior
\draw (m-2-7.south west) -- (m-2-7.south east);
\draw (m-2-6.north east) -- (m-2-6.south east);
%limite inferior
\draw (m-3-7.south west) -- (m-3-7.south east);
\draw (m-4-6.north east) -- (m-4-6.south east);

% Segunda fila
% Casilla 2-1
%limite superior
\draw (m-5-4.south west) -- (m-5-4.south east);
\draw (m-5-3.north east) -- (m-5-3.south east);
%limite inferior
\draw (m-6-4.south west) -- (m-6-4.south east);
\draw (m-7-3.north east) -- (m-7-3.south east);

% Casilla 2-3
%limite superior
\draw (m-5-10.south west) -- (m-5-10.south east);
\draw (m-5-9.north east) -- (m-5-9.south east);
%limite inferior
\draw (m-6-10.south west) -- (m-6-10.south east);
\draw (m-7-9.north east) -- (m-7-9.south east);

% Tercera fila
% Casilla 3-4
%limite superior
\draw (m-8-13.south west) -- (m-8-13.south east);
\draw (m-8-12.north east) -- (m-8-12.south east);


% Para dibujar cuadrados
%\node[draw=blue,rounded corners = 1ex,fit=(m-2-2)(m-4-4),inner sep = -5pt] {};



%\node[label=above right:$20$] at (m-2-3){33};

\end{tikzpicture}
\end{adjustbox}
\end{center}
\end{document}

答案1

以下是一个完整的示例,演示了@hpekristiansen 的建议。请注意

  • matrix of nodes被替换matrix of math nodes,使得中的每个节点\matrix在数学模式下都为否。
  • minimum width=1cm, minimum height=1cm被更紧凑的所取代minimum size=1cm
  • text height=1.5ex, text depth=.25ex添加,使用与pgf 手册text height/text depth,第 5.1 节。该节还解释了为什么需要选项来对齐节点文本。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\usepackage{adjustbox}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
%\usepackage[latin1]{inputenc}


\begin{document}

\begin{center}
    \begin{adjustbox}{max totalsize={.8\textwidth}{.8\textheight},center}   
\begin{tikzpicture}[
%row 1/.style={nodes={fill=yellow}},
%column 4/.style={nodes={fill=pink}},
%row 4 column 5/.style={nodes={fill=blue}}
]

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
  &    &    &    &    &        &    &    &        &    &    &    &    &     \\
  & 10 &    & 30 & 4  &        & 50 & 8  &        &    & 12 &    &    &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 125 \\
  &    &    & 10 &    &        & 30 &    &        &    &    &    &    &     \\
  & 4  &    & 40 & -4 &        &    & -4 &        & 35 & 10 &    &    &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 55  \\
  &    &    & 10 &    &        &    &    &        & 5  &    &    &    &     \\
  & 6  &    &    & 4  &        &    & 8  &        &    & -8 &    & 20 &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 30  \\
  &    &    &    &    &        &    &    &        &    &    &    &    &     \\
  &    & 90 &    &    & \le 60 &    &    & \le 65 &    &    & 20 &    &     \\
};


\draw (m-2-2.north west) rectangle (m-10-13.south east);

% Lineas horizontales
\draw (m-4-2.south west) -- (m-4-13.south east);
\draw (m-7-2.south west) -- (m-7-13.south east);

% Lineas verticales
\draw (m-2-4.north east) -- (m-10-4.south east);
\draw (m-2-7.north east) -- (m-10-7.south east);
\draw (m-2-10.north east) -- (m-10-10.south east);


\draw (m-4-4.north west) rectangle (m-4-4.south east);

\draw (m-2-4.south west) -- (m-2-4.south east);
\draw (m-2-3.north east) -- (m-2-3.south east);
%limite inferior
\draw (m-3-4.south west) -- (m-3-4.south east);
\draw (m-4-3.north east) -- (m-4-3.south east);

%limite superior
\draw (m-2-7.south west) -- (m-2-7.south east);
\draw (m-2-6.north east) -- (m-2-6.south east);
%limite inferior
\draw (m-3-7.south west) -- (m-3-7.south east);
\draw (m-4-6.north east) -- (m-4-6.south east);

% Segunda fila
% Casilla 2-1
%limite superior
\draw (m-5-4.south west) -- (m-5-4.south east);
\draw (m-5-3.north east) -- (m-5-3.south east);
%limite inferior
\draw (m-6-4.south west) -- (m-6-4.south east);
\draw (m-7-3.north east) -- (m-7-3.south east);

% Casilla 2-3
%limite superior
\draw (m-5-10.south west) -- (m-5-10.south east);
\draw (m-5-9.north east) -- (m-5-9.south east);
%limite inferior
\draw (m-6-10.south west) -- (m-6-10.south east);
\draw (m-7-9.north east) -- (m-7-9.south east);

% Tercera fila
% Casilla 3-4
%limite superior
\draw (m-8-13.south west) -- (m-8-13.south east);
\draw (m-8-12.north east) -- (m-8-12.south east);


% Para dibujar cuadrados
%\node[draw=blue,rounded corners = 1ex,fit=(m-2-2)(m-4-4),inner sep = -5pt] {};

%\node[label=above right:$20$] at (m-2-3){33};

\end{tikzpicture}
\end{adjustbox}
\end{center}

\end{document}

在此处输入图片描述

更新

一些改进:

  • 辅助选项“显示坐标”用于打印当前单元格相对于矩阵的坐标(这会减慢编译速度并加快以下绘图速度),以及
  • 一些\foreach循环使得线条绘制更加紧凑。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\usepackage{adjustbox}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
%\usepackage[latin1]{inputenc}

\tikzset{
  show coordinate/.style={
    execute option at begin maybe cell={
      label={[red, font=\tiny]center:(\the\pgfmatrixcurrentrow,\the\pgfmatrixcurrentcolumn)}
    }
  },
  execute option at begin maybe cell/.style={
    execute at begin cell={|[#1]|},
    execute at empty cell={\node[#1] {};}
  }
}

\begin{document}

\begin{center}
    \begin{adjustbox}{max totalsize={.8\textwidth}{.8\textheight},center}   
\begin{tikzpicture}[
%row 1/.style={nodes={fill=yellow}},
%column 4/.style={nodes={fill=pink}},
%row 4 column 5/.style={nodes={fill=blue}}
]

\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}, show coordinate]
{
  &    &    &    &    &        &    &    &        &    &    &    &    &     \\
  & 10 &    & 30 & 4  &        & 50 & 8  &        &    & 12 &    &    &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 125 \\
  &    &    & 10 &    &        & 30 &    &        &    &    &    &    &     \\
  & 4  &    & 40 & -4 &        &    & -4 &        & 35 & 10 &    &    &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 55  \\
  &    &    & 10 &    &        &    &    &        & 5  &    &    &    &     \\
  & 6  &    &    & 4  &        &    & 8  &        &    & -8 &    & 20 &     \\
  &    &    &    &    &        &    &    &        &    &    &    &    & 30  \\
  &    &    &    &    &        &    &    &        &    &    &    &    &     \\
  &    & 90 &    &    & \le 60 &    &    & \le 65 &    &    & 20 &    &     \\
};

% Lineas horizontales
\foreach \i in {1,4,7,10} {
  \draw (m-\i-2.south west) -- (m-\i-14.south west);
}

% Lineas verticales
\foreach \j in {1,4,7,10,13} {
  \draw (m-2-\j.north east) -- (m-11-\j.north east);
}

\foreach \k in {2-4, 2-7, 4-4, 4-7, 5-4, 5-10, 7-4, 7-10, 8-13} {
  \draw (m-\k.south west) rectangle (m-\k.north east);
}
\end{tikzpicture}
\end{adjustbox}
\end{center}

\end{document}

在此处输入图片描述

相关内容