我正在尝试在 Beamer 演示文稿中添加一个表格。请参阅以下代码:
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{array}
% break long word in tabular
% src: https://tex.stackexchange.com/a/338524
\newcolumntype{P}[1]{>{\hspace{0pt}}p{#1}}
\begin{document}
\begin{frame}{My Title}
\begin{table}
\newcommand{\ColWidth}{0.3\linewidth}
\begin{tabular}{|P{\ColWidth}|P{\ColWidth}|P{\ColWidth}|}
a\_long\_word\_without\_space & another\_long\_word\_without\_space & one\_more\_long\_word\_without\_space \\
A very very very unnecessarily long sentence & Another very very very unnecessarily long sentence & One more very very very unnecessarily long sentence \\
\end{tabular}
\caption{Wrap Text Inside Table}
\end{table}
\end{frame}
\end{document}
以下是生成的输出的屏幕截图:
如何启用表格内的文字换行?
答案1
我找到了一个解决方法。文本换行对于一个句子来说工作正常。但是,文本换行也应该配置为适用于下划线字符。请参阅下面的工作代码:
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{array}
% break long word in tabular
% src: https://tex.stackexchange.com/a/338524
\newcolumntype{P}[1]{>{\hspace{0pt}}p{#1}}
\begin{document}
\begin{frame}{My Title}
\begin{table}
\newcommand{\ColWidth}{0.3\linewidth}
% https://tex.stackexchange.com/a/9938
\renewcommand\_{\textunderscore\allowbreak}
\begin{tabular}{|P{\ColWidth}|P{\ColWidth}|P{\ColWidth}|}
a\_long\_word\_without\_space & another\_long\_word\_without\_space & one\_more\_long\_word\_without\_space \\
A very very very unnecessarily long sentence & Another very very very unnecessarily long sentence & One more very very very unnecessarily long sentence \\
\end{tabular}
\caption{Wrap Text Inside Table}
\end{table}
\end{frame}
\end{document}
请注意,我们允许在局部中断下划线字符,方法是将下划线定义如下\renewcommand\_{\textunderscore\allowbreak}
。此解决方法取自这里
下面是生成的 PDF 的屏幕截图:
答案2
图片来源:@musarithmia—— https://tex.stackexchange.com/a/196875/197451
要在表格列中换行,有两个简单的选项:
使用 tabular 并使用 指定其中一列为段落p{<width>}
。您必须指定该列的宽度,而其他列将适合内容的宽度。换行的p
列将始终左对齐。
\begin{tabular}{p{15mm} c c}
使用其他制表软件包之一。另一个答案演示了tabularx
,它扩展了列以填充整个表格的指定宽度。
使用tabulary
,您可以指定整个表格的宽度,然后您可以选择使用小写对齐命令 的传统表格样式列c, l, and r
;或者您可以使用大写命令来获取换行列。这样,您可以轻松创建具有任何对齐方式的换行列。
\begin{tabulary}{\textwidth}{C c c}
投影机示例:
第一个框架使用表格,第二个框架使用表格格式。
\documentclass{beamer}
\usepackage{tabulary}
\begin{document}
%*******************
\begin{frame}
\begin{table}
\begin{tabular}{p{15mm} | c | c | c | c }
Text that should wrap to multiple lines & Data & Data & Data & Data\\
\end{tabular}
\end{table}
\end{frame}
%*******************
\begin{frame}
\begin{table}
\begin{tabulary}{\textwidth}{C | c | c | c | c }
Text that should wrap to multiple lines & Data & Data & Data & Data\\
\end{tabulary}
\end{table}
\end{frame}
%*******************
\end{document}