我正在尝试使用 缩进表格内的文本\parbox
。根据 的高度,\parbox
到下一行的垂直距离会有所不同。标题和内容之间的垂直距离也会发生变化。
\documentclass{article}
\usepackage{calc}
\newcommand{\colwidth}{5cm}
\newcommand{\indentation}{1em}
\newcommand{\row}[2]{%
\textbf{#1}\par
\hspace*{\indentation}\parbox{\colwidth-\indentation}{#2}%
\\[.5em]%
}
\begin{document}
\begin{tabular}{p{\colwidth}}
\row{title}{short content}
\row{title}{content which is too long for one line}
\row{title}{short content again}
\row{title}{content which is too long for one line again}
\end{tabular}
\end{document}
为什么距离会变化?如何使它们保持不变?
答案1
您可以使用\parbox[t]
,但还有一种更简单的方法:
\documentclass{article}
\newcommand{\colwidth}{5cm}
\newcommand{\indentation}{1em}
\newcommand{\row}[2]{%
\hangindent=\indentation
\hangafter=1
\textbf{#1}\newline
#2\\[.5em]
}
\begin{document}
\begin{tabular}{p{\colwidth}}
\row{title}{short content}
\row{title}{content which is too long for one line}
\row{title}{short content again}
\row{title}{content which is too long for one line again}
\end{tabular}
\end{document}
或者,设置\leftskip
:
\documentclass{article}
\newcommand{\colwidth}{5cm}
\newcommand{\indentation}{1em}
\newcommand{\row}[2]{%
\leftskip=\indentation
\hspace*{-\indentation}\textbf{#1}\newline
#2\\[.5em]
}
\begin{document}
\begin{tabular}{p{\colwidth}}
\row{title}{short content}
\row{title}{content which is too long for one line}
\row{title}{short content again}
\row{title}{content which is too long for one line again}
\end{tabular}
\end{document}
答案2
我没有尝试在列内使用\parbox
es ,而是只将其作为2 列表的一部分,并将非缩进加粗的内容重叠。p
p
p
注意:最后显示缩进和列宽条以确认我已进行\tabcolsep
适当调整。
\documentclass{article}
\newcommand{\colwidth}{5cm}
\newcommand{\indentation}{1em}
\tabcolsep=.5\dimexpr\indentation\relax
\newcommand{\row}[2]{%
\rlap{\textbf{#1}}&\\
\\
}
\begin{document}
\noindent\begin{tabular}{@{}lp{\dimexpr\colwidth-2\tabcolsep}}
\row{title}{short content}
\row{title}{content which is too long for one line}
\row{title}{short content again}
\row{title}{content which is too long for one line again}
\end{tabular}
\bigskip
\noindent Indentation:\\
\noindent\rule{\indentation}{1pt}
\noindent Colwidth:\\
\noindent\rule{\colwidth}{1pt}
\end{document}