tikz:带有数学模式和绘图的表格

tikz:带有数学模式和绘图的表格

我制作了下表

在此处输入图片描述

使用LaTex以下代码:

\documentclass{article}

\begin{document}

\begin{table}[ph]
\protect\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{l|cccccc|c}
\hline 
 & \multicolumn{6}{c|}{Environment} & \tabularnewline
\cline{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean\tabularnewline
\hline 
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ & $\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$i$ & $\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ & $\overline{Y}_{i..}$\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$\tabularnewline
\hline 
Mean & $\overline{Y}_{.1.}$ & $\overline{Y}_{.2.}$ & $\ldots$ & $\overline{Y}_{.j.}$ & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$\tabularnewline
\hline 
\end{tabular}
\end{table}

\end{document}

现在我想在这个表格上画一些图,如下图所示:

在此处输入图片描述

我用于tikz此并且代码是:

\documentclass{article} % article standalone 
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=3.3em,align=center},
  text depth=0.25ex,
  text height=2ex,
  nodes in empty cells
  }
}

\begin{document}

\begin{table}
\begin{tikzpicture}
\matrix (m) [table] {
    %& K=3 & K=4 & K=5 & K=6 & K=7 & K=8 & K=9 & K=10 & K=11 & K=12\\
    \hline
 1  &  \overline{Y}_{11.}  &  \overline{Y}_{12.}  &  \ldots  &  \overline{Y}_{1j.}  &  \ldots  &  \overline{Y}_{1e.}  &  \overline{Y}_{1..} \\
 2  &  \overline{Y}_{21.}  &  \overline{Y}_{22.}  &  \ldots  &  \overline{Y}_{2j.}  &  \ldots  &  \overline{Y}_{2e.}  &  \overline{Y}_{2..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 i  &  \overline{Y}_{i1.}  &  \overline{Y}_{i2.}  &  \ldots  &  \overline{Y}_{ij.}  &  \ldots  &  \overline{Y}_{ie.}  &  \overline{Y}_{i..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 g  &  \overline{Y}_{g1.}  &  \overline{Y}_{g2.}  &  \ldots  &  \overline{Y}_{gj.}  &  \ldots  &  \overline{Y}_{ge.}  &  \overline{Y}_{g..} \\
\textrm{Mean} &  \overline{Y}_{.1.}  &  \overline{Y}_{.2.}  &  \ldots  &  \overline{Y}_{.j.}  &  \ldots  &  \overline{Y}_{.e.}  &  \overline{Y}_{...} \\
};
\begin{scope}[shorten >= 10pt,shorten <= 10pt]
\draw[red, thick] (m-4-2.west) -- (m-4-8.east);
\end{scope}
\end{tikzpicture}
\end{table}

\end{document}

在此处输入图片描述

任何帮助都将不胜感激。谢谢


已编辑


我想在 中使用这张表格beamer。第一帧中表格没有标记(手绘图中为红色),第二帧中表格有标记(手绘图中为红色)。那么最好的方法是什么呢?谢谢

答案1

相同的想法,但使用tikzmark库(如果需要,这可以让你轻松地在背景中放置一些填充颜色);我还使用了booktabs使表格看起来更专业。叠加只是利用\draw叠加感知这一事实来实现的。代码需要运行三次才能稳定:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{booktabs}
\usetikzlibrary{tikzmark}

\newcommand<>\FramedBox[3][]{%
\draw#4[red!80!black,rounded corners,fill=red!10,#1] 
  ([xshift=-1ex,yshift=3ex]pic cs:#2) 
    rectangle 
  ([xshift=1ex,yshift=-1ex]pic cs:#3);
}

\begin{document}

\begin{frame}
\begin{tikzpicture}[remember picture,overlay]
\FramedBox<2>{starta}{enda}
\FramedBox<3>{startc}{endc}
\FramedBox<4>[draw=cyan,fill=cyan!20]{startb}{endb}
\end{tikzpicture}

\begin{table}
\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{lccccccc}
\toprule 
 & \multicolumn{6}{c}{Environment} & 
\tabularnewline
\cmidrule{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean
\tabularnewline
\midrule
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ 
  & \tikzmark{startb}$\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$
\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ 
  & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$
\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ 
  & $\vdots$ & $\vdots$
\tabularnewline
$i$ & \tikzmark{starta}$\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ 
  & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ 
  & $\overline{Y}_{i..}$ \tikzmark{enda} 
\tabularnewline
$\vdots$ & $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ 
  & $\vdots$ & $\vdots$
\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ 
  & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$
\tabularnewline
\midrule
Mean & $\overline{Y}_{.1.}$ & \tikzmark{startc}$\overline{Y}_{.2.}$\tikzmark{endc} & $\ldots$ 
  & $\overline{Y}_{.j.}$\tikzmark{endb} & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$
\tabularnewline
\bottomrule 
\end{tabular}
\end{table}

\end{frame}

\end{document}

在此处输入图片描述

使用时,\tikzmark您为每个框架放置两个标记;一个是框架应该开始的地方,另一个是框架应该结束的地方。然后,您可以简单地使用环境\FramedBox中的命令tikzpicture来绘制框架;命令的语法是:

\FrameBox[<options for the frame>]{<start mark>}{<end mark>}

\FramedBox具有覆盖感知能力,如示例代码所示。

答案2

north west north east south east您快完成了。使用可以识别为单元格的4 个角south west。只需小心选择相应的单元格即可。

在此处输入图片描述

代码

\documentclass{article} % article standalone 
\usepackage[papersize={15cm,6cm}]{geometry}

\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\tikzset{ 
table/.style={
  matrix of math nodes,
  row sep=-\pgflinewidth,
  column sep=-\pgflinewidth,
  nodes={rectangle,draw=black,text width=3.3em,align=center},
  text depth=0.25ex,
  text height=2ex,
  nodes in empty cells
  }
}
\thispagestyle{empty}
\begin{document}

\begin{table}
\begin{tikzpicture}
\matrix (m) [table] {
    %& K=3 & K=4 & K=5 & K=6 & K=7 & K=8 & K=9 & K=10 & K=11 & K=12\\
    \hline
 1  &  \overline{Y}_{11.}  &  \overline{Y}_{12.}  &  \ldots  &  \overline{Y}_{1j.}  &  \ldots  &  \overline{Y}_{1e.}  &  \overline{Y}_{1..} \\
 2  &  \overline{Y}_{21.}  &  \overline{Y}_{22.}  &  \ldots  &  \overline{Y}_{2j.}  &  \ldots  &  \overline{Y}_{2e.}  &  \overline{Y}_{2..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 i  &  \overline{Y}_{i1.}  &  \overline{Y}_{i2.}  &  \ldots  &  \overline{Y}_{ij.}  &  \ldots  &  \overline{Y}_{ie.}  &  \overline{Y}_{i..} \\
 \vdots  &  \vdots  &  \vdots  &  \ddots  &  \vdots  &  \ddots  &  \vdots  &  \vdots \\
 g  &  \overline{Y}_{g1.}  &  \overline{Y}_{g2.}  &  \ldots  &  \overline{Y}_{gj.}  &  \ldots  &  \overline{Y}_{ge.}  &  \overline{Y}_{g..} \\
\textrm{Mean} &  \overline{Y}_{.1.}  &  \overline{Y}_{.2.}  &  \ldots  &  \overline{Y}_{.j.}  &  \ldots  &  \overline{Y}_{.e.}  &  \overline{Y}_{...} \\
};
\begin{scope}[shorten >= 10pt,shorten <= 10pt]
\draw[red, thick] (m-4-2.north west) -- (m-4-8.north east) -- (m-4-8.south east) -- (m-4-2.south west) -- cycle;
\end{scope}
\end{tikzpicture}
\end{table}
\end{document}

编辑:原作者的意图是在第一个图形上绘图。因此,这是一个解决方案,其中由命令定义的 4 个 tikzmarktikzmark被分配给第 i 行,并绘制一条具有样式的线来框住第 i 行。

在此处输入图片描述

代码

\documentclass{article}
\usepackage[papersize={15cm,10cm}]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\tikzset{line/.style ={draw, rounded corners=2pt, line width=1pt}}

\newcommand\tikzmark[1]{%
\tikz[remember picture]  \node[inner sep=0,outer sep=0] (#1){};%
}
\thispagestyle{empty}
\begin{document}

\begin{table}[ph]
\protect\caption{Genotype-by-Environment Means}
\centering{}%
\begin{tabular}{l|cccccc|c}
\hline 
 & \multicolumn{6}{c|}{Environment} & \tabularnewline
\cline{2-7} 
Genotype & $1$ & $2$ & $\ldots$ & $j$ & $\ldots$ & $e$ & Mean\tabularnewline
\hline 
$1$ & $\overline{Y}_{11.}$ & $\overline{Y}_{12.}$ & $\ldots$ & $\overline{Y}_{1j.}$ & $\ldots$ & $\overline{Y}_{1e.}$ & $\overline{Y}_{1..}$\tabularnewline
$2$ & $\overline{Y}_{21.}$ & $\overline{Y}_{22.}$ & $\ldots$ & $\overline{Y}_{2j.}$ & $\ldots$ & $\overline{Y}_{2e.}$ & $\overline{Y}_{2..}$\tabularnewline
$\vdots$& \tikzmark{a} $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tikzmark{b}\tabularnewline
$i$ & \tikzmark{d}$\overline{Y}_{i1.}$ & $\overline{Y}_{i2.}$ & $\ldots$ & $\overline{Y}_{ij.}$ & $\ldots$ & $\overline{Y}_{ie.}$ & $\overline{Y}_{i..}\tikzmark{c}$\tabularnewline
$\vdots$&  $\vdots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\ddots$ & $\vdots$ & $\vdots$\tabularnewline
$g$ & $\overline{Y}_{g1.}$ & $\overline{Y}_{g2.}$ & $\ldots$ & $\overline{Y}_{gj.}$ & $\ldots$ & $\overline{Y}_{ge.}$ & $\overline{Y}_{g..}$\tabularnewline
\hline 
Mean & $\overline{Y}_{.1.}$ & $\overline{Y}_{.2.}$ & $\ldots$ & $\overline{Y}_{.j.}$ & $\ldots$ & $\overline{Y}_{.e.}$ & $\overline{Y}_{...}$\tabularnewline
\hline 
\end{tabular}
\end{table}
\begin{tikzpicture}[remember picture,overlay]
\draw [red,line]($(a)+(-2.2ex,0)$)--($(b)+(2.5ex,0)$)--($(c)+(0.9ex,-1ex)$) -- ($(d)+(-0.9ex,-1ex)$)--cycle;
\end{tikzpicture}
\end{document}

相关内容