如何在同一列的表格单元格中垂直和水平对齐文本?

如何在同一列的表格单元格中垂直和水平对齐文本?

我想要顶部对齐右对齐某一列的所有 tabularx 单元格中的文本。到目前为止,我尝试过:

\documentclass[a4paper,10pt]{article}

\usepackage[top=2cm,bottom=2cm,right=2cm,left=3cm]{geometry}
\geometry{a4paper}
\usepackage{array}
\usepackage{tabularx}
\usepackage{ragged2e}
\newcolumntype{R}[1]{>{\RaggedLeft\arraybackslash\hspace{0pt}}p{#1}}

\begin{document}

\begin{tabularx}{\textwidth}{|R{2cm}|X|} \hline
A text on multiple lines & \shortstack{Line1 \\ Line2 \\ Line3 \\Line4 \\ Line5} \\ \hline
\end{tabularx}

\end{document}

输出如下:

在此处输入图片描述

\RaggedLeft命令似乎会覆盖数组包中列类型的顶部对齐p{'size'}。我该如何解决这个问题?

答案1

试验解决方案,因为我不知道第一列是否真的应该对齐:

\documentclass[a4paper,10pt]{article}

\usepackage[top=2cm,bottom=2cm,right=2cm,left=3cm]{geometry}
\geometry{a4paper}
\usepackage{array}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{multirow}
\usepackage{makecell}
\newcolumntype{R}[1]{>{\RaggedLeft}p{#1}}

\begin{document}

\begin{tabularx}{\textwidth}{|R{2cm}|X|} 
  \hline
  \multirow{5}{*}{\makecell[rt]{A text on\\ multiple lines}} & Line1 \tabularnewline
  & Line2  \tabularnewline 
  & Line3 \tabularnewline 
  & Line4 \tabularnewline 
  & Line5 \tabularnewline 
  \hline
\end{tabularx}

\begin{tabularx}{\textwidth}{|R{2cm}|X|} 
  \hline
  \multirowcell{1}[0pt][tR{2cm}]{A text on  multiple lines} & Line1 \tabularnewline
                                                            & Line2  \tabularnewline 
                                                            & Line3 \tabularnewline 
                                                            & Line4 \tabularnewline 
                                                            & Line5 \tabularnewline 
  \hline
\end{tabularx}

\end{document}

在此处输入图片描述

相关内容