如何使表格中的单个单元格垂直居中?

如何使表格中的单个单元格垂直居中?

我找不到使表格单元格内的单行垂直居中的方法。

看一下这个截图:

在此处输入图片描述

我想让文本在右下角单元格内垂直居中,但就是找不到方法。我试过,\usepackage{array}但不知道该如何将 应用于此\centering\arraybackslash单元格,它似乎可以在单元格内垂直对齐内容。

(我也不确定为什么这个单元格缺少右边框。)

以下是生成该表的 LaTeX 代码:

\documentclass{article}

\usepackage[left=1cm,top=1cm,right=1cm,bottom=1cm,nohead,nofoot]{geometry}

\usepackage[pdftex]{hyperref}
\usepackage{verbatim}
\usepackage{listings}
\usepackage{array}

\hypersetup{colorlinks}

\lstset{basicstyle=\ttfamily}

\pagestyle{empty}

% -----------------------------------------------------------------------

\begin{document}

\begin{tabular}{|p{5.5cm}|p{12.2cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\ 
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb|<< EOL| \\ 
\verb|foo| \\
\verb|bar| \\
\verb|baz| \\
\verb|EOL| & Redirect a bunch of lines to the stdin. \\
\hline
\end{tabular}

\end{document}

答案1

m您可以使用数组包将表格列中的材料垂直居中。但是,您还有另一个问题。使用\\不带前导符号的 会创建只有一列的行。这就是表格右边缘部分缺失的原因。相反,您可以使用\par插入换行符而不移动到表格的下一行。

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{|m{5.5cm}|m{12cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb+<< EOL+\par
\verb+foo+\par
\verb+bar+\par
\verb+baz+\par
\verb+EOL+
 & Redirect a bunch of lines to the stdin. \\
\hline
\end{tabular}
\end{document}

桌子

答案2

有一些错误。你用 来结束 tabularline \。应该是\\。另外你有两个列。无论数据如何,你都必须使用ampersands类似(比如说)

\verb|foo|& \

这就是您没有获得正确边界的原因。

要使特定单元格居中,您可以使用multirow。放入\usepackage{multirow}序言并使用

\verb+<< EOL+& \multirow{5}*{Redirect a bunch of lines to the stdin.} \\
    \verb+foo+&\\
    \verb+bar+&\\
    \verb+baz+&\\
    \verb+EOL+ &  \\
    \hline

完整代码:

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|p{5.5cm}|p{12cm}|}
\hline
\large\textbf{Redirection} & \large\textbf{Description} \\
\hline
\verb|> file| & Redirect standard output (stdout) to a file. \\
\hline
\verb|1> file| & Same as \verb|> file|. \verb|1| is the default file descriptor for stdout. \\
\hline
\verb|2> file| & Redirect standard error (stderr) to a file. \verb|2| is the default file descriptor for stderr. \\
\hline
\verb|>> file| & Append stdout to a file. \\
\hline
\verb|2>> file| & Append stderr to a file. \\
\hline
\verb|&> file| & Redirect stdout and stderr to a file. \\
\hline
\verb|>file 2>&1| & Another way to redirect both stdout and stderr to a file. This \textbf{is not} the same as \verb|2>&1 >file|. \textbf{Redirection order matters!} \\
\hline
\verb|> /dev/null| & Discard stdout. \\
\hline
\verb|2> /dev/null| & Discard stderr. \\
\hline
\verb|&> /dev/null| & Discard stdout and stderr. \\
\hline
\verb|< file| & Redirect the contents of the file to the stdin. \\
\hline
\verb+<< EOL+& \multirow{5}*{Redirect a bunch of lines to the stdin.} \\
\verb+foo+&\\
\verb+bar+&\\
\verb+baz+&\\
\verb+EOL+ &  \\
\hline
\end{tabular}
\end{document}

在此处输入图片描述

相关内容