![垂直对齐表格多列单元格中的文本](https://linux22.com/image/398738/%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E8%A1%A8%E6%A0%BC%E5%A4%9A%E5%88%97%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC.png)
我正在尝试重新创建第 4 页的最后一张表格这个文件,它看起来应该是这样的:
以下是我目前所掌握的信息:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{multirow}
\begin{document}
\setlength{\extrarowheight}{20pt}
\begin{center}
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{2}{| c }{Xenomai tasks} & \multicolumn{2}{ | c |}{Linux threads/processes} \\
\hline
\multicolumn{2}{| c }{Xenomai services} & \multicolumn{2}{ | c |}{Linux services} \\
\hline
\multirow{2}{*}{Real-time device driver} & Xenomai kernel & OS kernel & \multirow{2}{*}{Device driver} \\
\cline{2-3}
& \multicolumn{2}{c|}{Hardware Abstraction Layer (HAL)} & \\
\hline
\multicolumn{4}{| c |}{Adeos interrupt pipeline} \\
\hline
Specific hardware processes & \multicolumn{2}{ c |}{CPU + motherboard} & Specific hardware processes \\
\hline
\end{tabular}
\end{center}
\end{document}
目前,只有multirow
单元格在中间垂直和水平对齐。但是对于multicolumns
第一个包含文本的单元格,Xenomai tasks
我无法使文本垂直居中。
我尝试了其他帖子中提供的不同解决方案,但无法使其发挥作用。
例如,我尝试了以下操作:
\begin{tabular}{|m{10em}|c|c|m{10em}|}
是否有一种简单的方法可以使所有单元格中的文本对齐到中间?
答案1
您的表格单元格未垂直居中,因为\extrarowheight
似乎已添加到行的顶部。为了弥补这一点,您可以改用\\[...pt]
ot\\
或使用cellspace
包:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{multirow}
\usepackage{cellspace}
\setlength\cellspacetoplimit{10pt}
\setlength\cellspacebottomlimit{10pt}
\begin{document}
\begin{center}
\setlength\extrarowheight{5pt}
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{2}{| c }{Xenomai tasks} & \multicolumn{2}{ | c |}{Linux threads/processes} \\[5pt]
\hline
\multicolumn{2}{| c }{Xenomai services} & \multicolumn{2}{ | c |}{Linux services} \\[5pt]
\hline
\multirow{3}{*}{Real-time device driver} & Xenomai kernel & OS kernel & \multirow{3}{*}{Device driver} \\[5pt]
\cline{2-3}
& \multicolumn{2}{c|}{Hardware Abstraction Layer (HAL)} & \\[5pt]
\hline
\multicolumn{4}{| c |}{Adeos interrupt pipeline} \\[5pt]
\hline
Specific hardware processes & \multicolumn{2}{ c |}{CPU + motherboard} & Specific hardware processes \\[5pt]
\hline
\end{tabular}
\end{center}
\bigskip
\begin{center}
\begin{tabular}{|Sc|Sc|Sc|Sc|}
\hline
\multicolumn{2}{| Sc }{Xenomai tasks} & \multicolumn{2}{ | Sc |}{Linux threads/processes} \\
\hline
\multicolumn{2}{| Sc }{Xenomai services} & \multicolumn{2}{ | Sc |}{Linux services} \\
\hline
\multirow{3.5}{*}{Real-time device driver} & Xenomai kernel & OS kernel & \multirow{3.5}{*}{Device driver} \\
\cline{2-3}
& \multicolumn{2}{Sc|}{Hardware Abstraction Layer (HAL)} & \\
\hline
\multicolumn{4}{| Sc |}{Adeos interrupt pipeline} \\
\hline
Specific hardware processes & \multicolumn{2}{ Sc |}{CPU + motherboard} & Specific hardware processes \\
\hline
\end{tabular}
\end{center}
\end{document}
请记住,您的表格对于标准article
文档的文本宽度来说太宽了。为了改变这种情况,您可能需要在单元格中引入换行符。
答案2
由于您可以完全控制每个单元格calstable
,因此包卡路里非常适合这种类型的表格。要将文本垂直居中在单元格中,只需\vfil
在单元格内发出命令即可。
以下是所讨论表格的 MWE:
\documentclass{article}
\usepackage{cals, caption, xcolor}
\let\nc=\nullcell % CALS Shortcuts
\let\sc=\spancontent
\begin{document}
\small
\begin{calstable}[c] % Left aligned CALS tabular
% Calculate the column width
\colwidths{{\dimexpr(\columnwidth)/88*13\relax}
{\dimexpr(\columnwidth)/88*2\relax}
{\dimexpr(\columnwidth)/88*18\relax}
{\dimexpr(\columnwidth)/88*18\relax}
{\dimexpr(\columnwidth)/88*2\relax}
{\dimexpr(\columnwidth)/88*13\relax}
{\dimexpr(\columnwidth)/88*14\relax}
}
% The tabular fills the text area if sum of all columns is 88
% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt} % Outside frame rules
\def\cals@framecs@width{0,4pt}
\def\cals@bodyrs@width{0.4pt} % Rule between header and tabular body
\def\cals@cs@width{0.4pt} % Inside vertical rules
\def\cals@rs@width{0.4pt} % Inside horizontal rules
\def\cals@bgcolor{}
\def\bb{\ifx\cals@borderB\relax % Bottom border switch (off-on)
\def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}
\def\tb{\ifx\cals@borderT\relax % Top border switch (off-on)
\def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}
\def\lb{\ifx\cals@borderL\relax % Left border switch (off-on)
\def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}
\def\rb{\ifx\cals@borderR\relax % Right border switch (off-on)
\def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}
\def\green{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{green!80}
\else \def\cals@bgcolor{} \fi}
\def\lgreen{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{green!15}
\else \def\cals@bgcolor{} \fi}
\def\dgreen{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{green!80!black}
\else \def\cals@bgcolor{} \fi}
\def\red{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{red!80}
\else \def\cals@bgcolor{} \fi}
\def\orange{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{orange!80}
\else \def\cals@bgcolor{} \fi}
\def\lorange{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{orange!30}
\else \def\cals@bgcolor{} \fi}
\def\yellow{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{yellow!80}
\else \def\cals@bgcolor{} \fi}
\def\plum{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{blue!30!red!30}
\else \def\cals@bgcolor{} \fi}
\def\violet{\ifx\cals@bgcolor\empty % "Switch" to turn on and off colour
\def\cals@bgcolor{violet!80}
\else \def\cals@bgcolor{} \fi}
% R1H1
\brow
\green\nc{ltb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Xenomai tasks}
\lb\nc{ltb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Linux threads/processes}\lb\green
\tb\rb\alignL\cell{\vfil user mode}\tb\rb
\erow
%R3B2
\brow
\red\nc{ltb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Xenomai services}\red
\orange\nc{ltb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Linux services}\orange
\tb\rb\alignL\cell{\vfil kernel mode}\tb\rb
\erow
%R4B3
\brow
\lgreen\nc{lrt}
\lb\cell{}\lb\lgreen
\yellow\alignC\cell{\vfil Xenomai kernel}
\alignC\cell{\vfil OS kernel}\yellow
\lgreen\rb\cell{}\rb
\nc{lrt}\lgreen
\tb\rb\nc{lrt}\tb\rb
\erow
%R5B4
\brow
\lgreen\nc{lrb}\alignL\sc{\vfil real-time\\devic\\driver}\lgreen
\lorange\nc{ltb}
\nc{tb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Hardware Abstraction Layer (HAL)}\lorange
\lgreen\nc{lrb}\alignR\sc{\vfil device driver}\lgreen
\tb\rb\alignL\nc{lrb}\sc{\vfil software}\tb\rb
\erow
%R6B5
\brow
\dgreen\nc{ltb}
\nc{tb}
\nc{tb}
\nc{tb}
\nc{tb}
\nc{rtb}\alignC\sc{\vfil Adeos (interrupt pipeline)}\dgreen
\tb\rb\alignL\cell{}\tb\rb
\erow
%R7B6
\brow
\plum\nc{ltb}
\nc{rtb}\alignC\sc{\vfil specific hardware processes}\plum
\violet\nc{ltb}
\nc{rtb}\alignC\sc{\vfil CPU + motherboard}\violet
\plum\nc{ltb}
\nc{rtb}\alignC\sc{\vfil specific hardware processes}\plum
\bb\tb\rb\alignL\cell{\vfil hardware}\tb\rb\bb
\erow
\makeatletter
\end{calstable}\par
\end{document}