创建一个表格,其中单元格内的图像居中且标签旋转

创建一个表格,其中单元格内的图像居中且标签旋转

在此处输入图片描述

我想修复上面的表格以便:

  1. 数字垂直居中(“低”和“高”标签也是如此)
  2. 右侧的图形没有剪掉上面的分界线
  3. 外部标签(即“这是 x 轴”和“这是 y 轴”)相对于内部标签居中(例如,“这是 x 轴”相对于“左”和“右”居中;与“这是 y 轴”相同)
  4. 理想情况下,代码应该适用于任意大小的图形(即无需手动调整)

生成表格的 LaTeX 代码如下:

\documentclass[letter]{article}
\usepackage{graphicx,array,multirow}

\begin{document}

\begin{tabular}{cccc}
  \cline{3-4}
  \multirow{2}{*}{\rotatebox[origin=c]{90}{This is the $y$-axis}} & \rotatebox[origin=c]{90}{High}   & \multicolumn{1}{|c}{\includegraphics[scale=0.25]{fig.jpg}
} & \multicolumn{1}{|c|}{\includegraphics[scale=0.65]{fig.jpg}} \\
  \cline{3-4}
                      & \rotatebox[origin=c]{90}{Low}  & \multicolumn{1}{|c}{\includegraphics[scale=0.25]{fig.jpg}} & \multicolumn{1}{|c|}{\includegraphics[scale=0.35]{fig.jpg}} \\
  \cline{3-4}
                      &       & Left     &     Right \\
                      &       & \multicolumn{2}{c}{This is the $x$-axis} \\
\end{tabular}

\end{document}

使用的示例图(fig.jpg)如下:

图.jpg

有任何想法吗?

答案1

可以用 来完成TikZ matrix。它解决了垂直和水平图形对齐的问题,也解决了轴标签的问题,因为它们可以作为两个label节点放置。

主要问题是如何绘制边框线,因为matrix只放置节点,但它们保留其实际大小和边框。因此,一旦matrix完成,我们需要一些命令来绘制垂直和水平线。

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning,matrix}

\begin{document}

\begin{tikzpicture}
\matrix (A) [matrix of nodes,
column sep=2mm, row sep=2mm, nodes={anchor=center},
column 1/.style={nodes={anchor=center, rotate=90}},
row 3/.style={nodes={anchor=base}},
label={[rotate=90, anchor=south]left:This is the $y$-axis}, 
label={below:This is the $x$-axis}]
{
High & \includegraphics[scale=.25]{fig.jpg} & \includegraphics[scale=.65]{fig.jpg} \\
Low & \includegraphics[scale=.25]{fig.jpg} & \includegraphics[scale=.35]{fig.jpg} \\
& Left & Right\\
};

\draw (A-1-3.north east) rectangle (A-1-2.west|-A-2-3.south);
\path (A-1-2.east) -- coordinate (aux1) (A-1-3.west);
\path (A-1-3.south) -- coordinate (aux2) (A-2-3.north);

\draw (A-1-3.north-|aux1)--(A-2-3.south-|aux1);
\draw (A-2-2.west|-aux2)--(A-1-3.east|-aux2);

\end{tikzpicture}
\end{document}

在此处输入图片描述

更新

对于#3,我们可以抑制matrix labels(根据整个矩阵大小居中)并使用positioning放置行和列标签的引用的常规节点(带有库):

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning,matrix}

\begin{document}

\begin{tikzpicture}
\matrix (A) [matrix of nodes,
column sep=2mm, row sep=2mm, nodes={anchor=center},
column 1/.style={nodes={anchor=center, rotate=90}},
row 3/.style={nodes={anchor=base}}]
{
High & \includegraphics[scale=.25]{fig.jpg} & \includegraphics[scale=.65]{fig.jpg} \\
Low & \includegraphics[scale=.25]{fig.jpg} & \includegraphics[scale=.35]{fig.jpg} \\
& Left & Right\\
};

\path (A-1-1) -- node[rotate=90, left=5mm, anchor=center] {This is the $y$-axis} (A-2-1);
\path (A-3-2) -- node[below=5mm, anchor=center] {This is the $x$-axis} (A-3-3);

\draw (A-1-3.north east) rectangle (A-1-2.west|-A-2-3.south);
\path (A-1-2.east) -- coordinate (aux1) (A-1-3.west);
\path (A-1-3.south) -- coordinate (aux2) (A-2-3.north);

\draw (A-1-3.north-|aux1)--(A-2-3.south-|aux1);
\draw (A-2-2.west|-aux2)--(A-1-3.east|-aux2);

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

可以使用\raiseboxcellspace垂直填充包来实现。如下所示:

\documentclass[letter]{article}
\usepackage{graphicx,array,multirow}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}


\begin{document}

\begin{tabular}{cc|Sc|Sc|}
  \cline{3-4}
  \multirow{2}{*}{\rotatebox[origin=c]{90}{This is the $y$-axis}} & \rotatebox[origin=c]{90}{High} & \raisebox{-0.5\height}{\includegraphics[scale=0.25]{ball.jpg}} & \raisebox{-0.5\height}{\includegraphics[scale=0.35]{ball.jpg}} \\
  \cline{3-4}
    & \rotatebox[origin=c]{90}{High} & \raisebox{-0.5\height}{\includegraphics[scale=0.25]{ball.jpg}} & \raisebox{-0.5\height}{\includegraphics[scale=0.35]{ball.jpg}} \\
  \cline{3-4}
  \multicolumn{2}{c}{}   & \multicolumn{1}{c}{Small} & \multicolumn{1}{c}{Large} \\
  \multicolumn{2}{c}{} & \multicolumn{2}{c}{This is the $x$-axis} \\
\end{tabular}

\end{document} 

在此处输入图片描述

答案3

由于其他答案都没有解决第 3 点,因此这里有一个使用页面锚点的 ConTeXt 解决方案。基本思路是在单词“High”和“Low”处设置锚点,然后使用位置图形在两个锚点的中点处绘制“这是 $y$ 轴”。

\useexternalfigure[ball][https://i.stack.imgur.com/6Ap04.jpg]
\startsetups align
  \setupTABLE[align={lohi,middle},offset=0.25em]
  \setupTABLE[column][first][frame=off]
  \setupTABLE[row][last][frame=off]
\stopsetups

\defineframedtext
  [labels]
  [background=labels,
   loffset=1.5\lineheight,
   roffset=0pt,
   boffset=1.5\lineheight,
   toffset=0pt,
   width=fit,
   offset=overlay,
   frame=off,
  ]

\startMPinclusions
  input mp-abck.mpiv;
\stopMPinclusions

\defineoverlay[labels][\positionoverlay{labels}]

\startpositionoverlay{labels}
  \setMPpositiongraphic{high}{y:label}{to=low}
  \setMPpositiongraphic{left}{x:label}{to=right}
\stoppositionoverlay

\startMPpositiongraphic{y:label}
  newpair p, q;
  initialize_box(\MPpos{\MPvar{self}}); p := 0.5[llxy, ulxy];
  initialize_box(\MPpos{\MPvar{to}});   q := 0.5[llxy, ulxy];

  label.lft("\rotate{This is $y$-axis}", 0.5[p,q] - (0.5LineHeight,0));
  anchor_box(\MPanchor{\MPvar{self}});
\stopMPpositiongraphic

\startMPpositiongraphic{x:label}
  newpair p, q;
  initialize_box(\MPpos{\MPvar{self}}); p := 0.5[llxy, lrxy];
  initialize_box(\MPpos{\MPvar{to}});   q := 0.5[llxy, lrxy];

  label.bot("This is $x$-axis", 0.5[p,q] - (0,0.5LineHeight));
  anchor_box(\MPanchor{\MPvar{self}});
\stopMPpositiongraphic


\starttext
\startlabels
\startTABLE[setups=align]
  \NC \hpos{high}{\rotate{High}}
  \NC \dontleavehmode\externalfigure[ball][width=1.5cm]
  \NC \dontleavehmode\externalfigure[ball][width=5.5cm]
  \NC \NR
  \NC \hpos{low}{\rotate{Low}}
  \NC \dontleavehmode\externalfigure[ball][width=1.5cm]
  \NC \dontleavehmode\externalfigure[ball][width=2.5cm]
  \NC \NR
  \NC \NC \hpos{left}{Left} \NC \hpos{right}{Right} \NC \NR
\stopTABLE
\stoplabels
\stoptext

这使

以下是几个不同尺寸的球的结果:

相关内容