所以我有一张像这样的表格:
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{tabular}{C{3cm}C{2.3cm}C{2.3cm}C{2.3cm}}
& \textbf{Case 1} & \textbf{Case 2} & \textbf{Case 3} \\
\textbf{Scenario 1}& \cellcolor[HTML]{158B00} & \cellcolor[HTML]{55AA00} & \cellcolor[HTML]{95CA00} \\ [2.3cm]
\textbf{Scenario 2} & \cellcolor[HTML]{008000} & \cellcolor[HTML]{40A000} & \cellcolor[HTML]{80C000} \\[2.3cm]
%\multicolumn{1}{l!{\color{red}\vrule width 4pt}}{\cellcolor[HTML]{80C000}} \\[2.3cm]
\textbf{Scenario 3} & \cellcolor[HTML]{40a000} & \cellcolor[HTML]{55aa00} & \cellcolor[HTML]{6ab500} \\[2.3cm]
\end{tabular}
我想垂直居中场景 1、场景 2、场景 3但许多方法都失败了,例如米。这是因为表格中定义的参数吗?你知道我该怎么做吗?
答案1
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[!h]
\newcommand\mc[1]{\multicolumn{1}{c}{ #1}}
\begin{tabular}{p{3cm}p{2.3cm}p{2.3cm}p{2.3cm}}
&\mc{ \textbf{Case 1}} & \mc{\textbf{Case 2}} & \mc{\textbf{Case 3}} \\
\textbf{Scenario 1}& \cellcolor[HTML]{158B00} & \cellcolor[HTML]{55AA00} &
\cellcolor[HTML]{95CA00} \\ [2.3cm]
\textbf{Scenario 2} & \cellcolor[HTML]{008000} & \cellcolor[HTML]{40A000} &
\cellcolor[HTML]{80C000} \\[2.3cm]
%\multicolumn{1}{l!{\color{red}\vrule width 4pt}}{\cellcolor[HTML]{80C000}}
\\[2.3cm]
\textbf{Scenario 3} & \cellcolor[HTML]{40a000} & \cellcolor[HTML]{55aa00} &
\cellcolor[HTML]{6ab500} \\[2.3cm]
\end{tabular}
\end{table}
\end{document}
编辑——重新定义第一列内容的居中
正如您所猜测的,m 类型的列没有效果,因为在行尾手动添加了 2.3 厘米 - 所以我在第一列中添加了一条不可见的规则,但现在居中已生效 - 这可以通过行尾的第二条规则看到,黑色垂直规则已添加 - 稍后可以将其删除,仅用于演示目的 - 现在第一列居中,可以删除 2.3 厘米 - 您可以根据需要扩展第一列中的规则
平均能量损失
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}[!h]
\newcommand\mc[1]{\multicolumn{1}{c}{ #1}}
\newcolumntype{C}{ >{\centering\arraybackslash} m{3cm} }
\newcommand{\myrule}{\vrule width 3pt}
\begin{tabular}{ CCCC}
&\mc{ \textbf{Case 1}} & \mc{\textbf{Case 2}} &
\mc{\textbf{Case 3}} \myrule\\
\rule[-20pt]{0pt}{46pt} Scenario 1& \cellcolor[HTML]{158B00} &
\cellcolor[HTML]{55AA00} & \cellcolor[HTML]{95CA00} \myrule \\
Scenario 2 & \cellcolor[HTML]{008000} & \cellcolor[HTML]{40A000} &
\cellcolor[HTML]{80C000} \\[2.3cm]
Scenario 3 & \cellcolor[HTML]{40a000} & \cellcolor[HTML]{55aa00} &
\cellcolor[HTML]{6ab500} \\[2.3cm]
\end{tabular}
\end{table}
\end{document}
最后结果
\begin{tabular}{ CCCC}
&\mc{ \textbf{Case 1}} & \mc{\textbf{Case 2}} &
\mc{\textbf{Case 3}} \\
\rule[-27pt]{0pt}{70pt} Scenario 1& \cellcolor[HTML]{158B00} &
\cellcolor[HTML]{55AA00} & \cellcolor[HTML]{95CA00} \\
\rule[-27pt]{0pt}{70pt} Scenario 2 & \cellcolor[HTML]{008000} &
\cellcolor[HTML]{40A000} & \cellcolor[HTML]{80C000} \\
\rule[-27pt]{0pt}{70pt} Scenario 3 & \cellcolor[HTML]{40a000} &
\cellcolor[HTML]{55aa00} & \cellcolor[HTML]{6ab500} \\
\end{tabular}
编辑以适应方形电池
第一个单元格中插入的规则是,每列比基线低 40 pt,每个字符约为 6pt,因此,低 40 pt+高 6 pt+高 40 pt 总共为 86 pt,相当于 3.022 厘米,如果每个颜色单元格的宽度为 3 厘米,则意味着正方形(大约)的宽度相差 0.022
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother
\begin{document}
\newdimen\mylength
\mylength=86pt
\convertto{cm}{\the\mylength} cm
\begin{table}[!h]
\newcommand\mc[1]{\multicolumn{1}{c}{ #1}}
\newcolumntype{C}{ >{\centering\arraybackslash} m{3cm} }
%\newcommand{\myrule}{\vrule width 3pt}
\begin{tabular}{ CCCC}
&\mc{ \textbf{Case 1}} & \mc{\textbf{Case 2}} &
\mc{\textbf{Case 3}} \\
\rule[-40pt]{0pt}{86pt} Scenario 1& \cellcolor[HTML]{158B00} &
\cellcolor[HTML]{55AA00} & \cellcolor[HTML]{95CA00} \\
\rule[-40pt]{0pt}{86pt} Scenario 2 & \cellcolor[HTML]{008000} & \cellcolor[HTML]{40A000} &
\cellcolor[HTML]{80C000} \\[2.3cm]
\rule[-40pt]{0pt}{86pt} Scenario 3 & \cellcolor[HTML]{40a000} & \cellcolor[HTML]{55aa00} &
\cellcolor[HTML]{6ab500} \\[2.3cm]
\end{tabular}
\end{table}
\end{document}