![需要将 Latex 表中的文本对齐到单元格的中间](https://linux22.com/image/472401/%E9%9C%80%E8%A6%81%E5%B0%86%20Latex%20%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%E5%AF%B9%E9%BD%90%E5%88%B0%E5%8D%95%E5%85%83%E6%A0%BC%E7%9A%84%E4%B8%AD%E9%97%B4.png)
我正在尝试将文本中心(水平或垂直)放置在 latex 表格单元格中(见最后一张图片)。有趣的是,https://www.papeeria.com
当我使用 TeXLive 2016 发行版时,代码可以在在线编辑器(例如)中使用,但在最新的发行版 2019 中却不行。它在 texmaker 中不起作用。我已经重新安装了 texmaker 和 LaTeX。
我的代码是:
\documentclass[landscape]{article}
\usepackage{array}
\usepackage{geometry}
\geometry{a4paper, left =0.85cm, right= 0.85cm, top = 1.55cm, bottom = 1.55cm}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{N}{@{}m{0pt}@{}}
\usepackage{array} % for the first table
\usepackage{xcolor,colortbl} % <--- color cell in table
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|N}
\hline
\cellcolor{orange!50} \huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\[85mm]
\hline
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\ [85mm]
\hline
\end{tabular}
\end{table}
\newpage
\begin{table}[ht]
\centering
\begin{tabular}{|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|N}
\hline
\cellcolor{orange!50} \huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\[85mm]
\hline
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\ [85mm]
\hline
\end{tabular}
\end{table}
\end{document}
但我想要的是这个:
答案1
您可以使用具有定义高度的空列来伪造垂直高度。以下将您的(未使用,为什么要添加它们?)N
类型列替换为H{85mm}
,这是一个垂直居中的列,高度为 85 毫米。
\documentclass[landscape]{article}
\usepackage{array}
\usepackage{geometry}
\geometry{a4paper, left =0.85cm, right= 0.85cm, top = 1.55cm, bottom = 1.55cm}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{N}{@{}m{0pt}@{}}
\newcolumntype{H}[1]{@{}>{\rule{0pt}{#1}}m{0pt}@{}}
\usepackage{array} % for the first table
\usepackage{xcolor,colortbl} % <--- color cell in table
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|H{85mm}}
\hline
\cellcolor{orange!50} \huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\
\hline
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\
\hline
\end{tabular}
\end{table}
\newpage
\begin{table}[ht]
\centering
\begin{tabular}{|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|H{85mm}}
\hline
\cellcolor{orange!50} \huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\
\hline
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\\
\hline
\end{tabular}
\end{table}
\end{document}
如果您不希望表格的每一行都产生这种效果,您可以切换回旧N
类型的列并使用其中的显式值\rule{0pt}{<height>}
来设置该行的高度。
答案2
\parbox
允许您指定高度和内部对齐,而p{}
列及其变体则不可以。您需要 collcell 包才能使用它们。
\documentclass[landscape]{article}
\usepackage{geometry}
\geometry{a4paper, left =0.85cm, right= 0.85cm, top = 1.55cm, bottom = 1.55cm}
\usepackage{array}
\usepackage{collcell}
\usepackage{xcolor,colortbl} % <--- color cell in table
\newcommand{\mycolumn}[1]{\parbox[c][85mm][c]{\myarg}{\centering #1}}
\newcolumntype{M}[1]{>{\def\myarg{#1}\collectcell\mycolumn}{l}<{\endcollectcell}}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|M{5.1cm}|}
\hline
\cellcolor{orange!50} \huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text \\
\hline
\huge Text &
\huge Text &
\huge Text &
\huge Text &
\huge Text \\
\hline
\end{tabular}
\end{table}
\end{document}
答案3
你可以使用该tabularray
包,然后你的文本将自动垂直居中:
\documentclass[landscape]{article}
\usepackage{geometry}
\geometry{a4paper, hmargin=0.85cm, vmargin=1.55cm}
\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
\begin{table}[ht]
\centering
\begin{tblr}{
vlines,
hlines,
colspec={XXXXX},
rows={ht=8.5cm,halign=c,font=\huge},
cell{1}{1}={bg=orange!50}
}
Text & Text & Text & Text & Text\\
Text & Text & Text & Text & Text\\
\end{tblr}
\end{table}
\end{document}