在表格的单个单元格中悬挂缩进并对齐多行文本

在表格的单个单元格中悬挂缩进并对齐多行文本

我有相当多的文本需要在表格的单个单元格中以可读的方式进行格式化,因此我想使用悬挂缩进。但除此之外,我还想使文本对齐,但这不起作用。如果我使用\usepackage{ragged2e}然后更改\raggedright\justify,一切都会变得一团糟。

为了获得 hangindent,我需要发出一个\customnewline命令来重复 hangindent(参见左列和右列之间的区别)。

我怎样才能将这一切结合起来,让文本“悬挂”并对齐?

注意:这是一个相当大的表,我使用它来创建Excel 转 LaTeX,所以我想尽可能少地做更改...这就是我改变列类型的原因。

\documentclass[a4paper,10pt]{article}

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{tabularx}

\begin{document}

% define new column type
\newcolumntype{Y}[1]{>{\small \hangindent=1em \raggedright \let\newline\\\arraybackslash}p{#1}}

% define newline to use hangindent on new line
\newcommand{\customnewline}{\newline \hangindent=1em}

\renewcommand{\arraystretch}{1.3}


\begin{table}[htbp]
  \centering
  \caption{Example}
    \begin{tabular}{Y{15em}Y{15em}}
    \toprule
    \multicolumn{1}{c}{\textbf{customnewline}} & \multicolumn{1}{c}{\textbf{normal newline}} \\
    \midrule
    A lot of text that I want to justify and hangindent \customnewline And there some more text in the same cell that I want to hangindent as well & A lot of text that I want to justify and hangindent \newline And there some more text in the same cell that I want to hangindent as well \\
    A lot of text that I want to justify and hangindent \customnewline And there some more text in the same cell that I want to hangindent as well & A lot of text that I want to justify and hangindent \newline And there some more text in the same cell that I want to hangindent as well \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

\end{document}

表 MVCE 示例

答案1

您可以使用空白行,只要您为每个段落设置悬挂缩进即可:

\documentclass[a4paper,10pt]{article}

\usepackage{booktabs,array}

\begin{document}

% define new column type
\newcolumntype{Y}[1]{%
  >{\small\raggedright\everypar{\hangindent=1em}\arraybackslash}p{#1}%
}

\newcolumntype{Z}[1]{%
  >{\small\everypar{\hangindent=1em}\arraybackslash}p{#1}%
}

% define newline to use hangindent on new line

\renewcommand{\arraystretch}{1.3}

\noindent
\begin{tabular}{Y{15em}Z{15em}}
\toprule
\multicolumn{1}{c}{Ragged right} &
\multicolumn{1}{c}{Justified} \\
\midrule
A lot of text that I want to justify and hangindent

And there some more text in the same cell that I want to 
hangindent as well
&
A lot of text that I want to justify and hangindent

And there some more text in the same cell that I want to 
hangindent as well
\\
\midrule
A lot of text that I want to justify and hangindent

And there some more text in the same cell that I want to 
hangindent as well
&
A lot of text that I want to justify and hangindent

And there some more text in the same cell that I want to 
hangindent as well
\\
\bottomrule
\end{tabular}

\end{document}

在此处输入图片描述

相关内容