如何用 TikZ 绘制这些图像?

如何用 TikZ 绘制这些图像?

我曾经需要使用这些图形: 第一 第二个

这似乎是一张简单的桌子,但我无法

  • 跨表格线随机绘制,但要考虑到单元格的坐标(如第一张图片所示)
  • 设置偏移量以区分实线和虚线(第二张图)

我现在不需要使用这些图形,但我发现不知道如何做到这一点让我很烦恼。我相信有人可以在 TikZ 中完全绘制它们,但也许可以将 TikZ 与表格结合起来,这样就不必担心复杂的坐标计算了。

有人能给出提示或解决方案吗?我对 TikZ 的了解相当少,而且仅仅是理论性的。

答案1

这是使用 的一种可能性\matrix。代码中有一些注释,如果不清楚的话可以询问。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

% hack for style of empty cells from https://tex.stackexchange.com/a/386805/ 
\tikzset{empty node/.style={coordinate}}
\makeatletter
\def\tikz@lib@matrix@empty@cell{\iftikz@lib@matrix@empty\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
\begin{tikzpicture}

\matrix [
  % with the following option each cell becomes a node, set in math mode
   % the nodes are named automatically as <matrix name>-<row number>-<column number>
   matrix of math nodes, 
   nodes in empty cells, % add cells also to empty nodes
   nodes={minimum width=1cm, minimum height=1cm, font=\strut} % set style of nodes
] (m) % give the matrix the name m
{
 & \mu = 0 & 1 & 2 & 3 & \dots \\
\nu = 0 & & & & & \\
1 & & & & & \\
2 & & & & & \\
3 & & & & & \\
\vdots & & & & &  \\
};

% for explanation of |- syntax: https://tex.stackexchange.com/a/401429/
% used to ensure horizontal/vertical lines
\draw (m-1-2.north west) -- (m-1-2.north west |- m-6-1.south east);
\draw (m-2-1.north west) -- (m-2-1.north west -| m-1-6.south east);

% due to the automatic node naming mentioned above, m-1-2 is the
% node in the first row, second column of the matrix

% a plot is an easy way of drawing a line with dots
\draw plot[mark=*] coordinates {(m-2-2)(m-2-3)(m-2-4)(m-3-4)(m-3-5)};


% for explanation of |- syntax: https://tex.stackexchange.com/a/401429/
% using that you only need to specify every other corner on the path
\draw (m-3-2) |- (m-4-3) |- (m-6-6);

% draw shifted, dotted line
% transform canvas idea from https://tex.stackexchange.com/a/180669/ 
\draw [dotted, transform canvas={shift={(5pt,5pt)}}] (m-3-2) |- (m-4-3) |- (m-6-6);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用{NiceArray}nicematrixTikz 使用 创建的 Tikz 节点绘制折线nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\[
\begin{NiceArray}{c|ccccc}
           & \mu = 0 & 1 & 2 & 3 & \cdots \\
   \hline
   \nu = 0 \\
   1       \\
   2       \\
   3       \\
   \vdots  
\CodeAfter
   \tikz [radius=2pt,every circle/.style = fill]
    \draw plot [mark = *] coordinates 
      {(2.5-|2.5)(2.5-|3.5)(2.5-|4.5)(3.5-|4.5)(3.5-|5.5)(4.5-|5.5)(4.5-|6.5)(5.5-|6.5)} ;
\end{NiceArray}
\]


\[
\begin{NiceArray}{c|ccccccc}
          & \mu = 0 & 1 & 2 & 3 & \cdot & \cdot & \cdot \\
  \hline
  \nu = 0 \\
  1       \\
  2       \\
  \cdot   \\
  \cdot   \\
  \cdot   \\
\CodeAfter
  \begin{tikzpicture}
    \draw (2.5-|2.5) -- (2.5-|4.5) |- (3.5-|5.5) |- (4.5-|6.5) |- (5.5-|7.5) ; 
    \draw (2.5-|2.5) -- (5.5-|2.5) -| (6.5-|3.5) -| (7.5-|4.5) ; 
    \draw [dotted] 
          ([xshift=1mm]2.5-|2.5) |- (3.5-|3.5) |- (4.5-|4.5) |- (5.5-|5.5) |- (6.5-|6.5) ; 
  \end{tikzpicture}
\end{NiceArray}
\]

\end{document}

上述代码的输出

相关内容