![创建一个表格,其中单元格内的图像居中且标签旋转](https://linux22.com/image/338638/%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E8%A1%A8%E6%A0%BC%EF%BC%8C%E5%85%B6%E4%B8%AD%E5%8D%95%E5%85%83%E6%A0%BC%E5%86%85%E7%9A%84%E5%9B%BE%E5%83%8F%E5%B1%85%E4%B8%AD%E4%B8%94%E6%A0%87%E7%AD%BE%E6%97%8B%E8%BD%AC.png)
我想修复上面的表格以便:
- 数字垂直居中(“低”和“高”标签也是如此)
- 右侧的图形没有剪掉上面的分界线
- 外部标签(即“这是 x 轴”和“这是 y 轴”)相对于内部标签居中(例如,“这是 x 轴”相对于“左”和“右”居中;与“这是 y 轴”相同)
- 理想情况下,代码应该适用于任意大小的图形(即无需手动调整)
生成表格的 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)如下:
有任何想法吗?
答案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
可以使用\raisebox
和cellspace
垂直填充包来实现。如下所示:
\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
这使
以下是几个不同尺寸的球的结果: