1

I am trying to add a table in my beamer presentation. Please see the code below:

\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}

Below is the screenshot of generated output:

enter image description here

How to enable text wrapping inside a table?

ravi
  • 1,618

2 Answers2

1

I found a workaround to the problem. The text wrapping is working fine for a sentence. However, text wrapping should be configured to work for underscore character too. Please see below the working code:

\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}

Please note that we allowed breaking on underscore characters locally by defining underscore as following \renewcommand\_{\textunderscore\allowbreak}. This workaround is taken from here

Below is a screenshot of the generated PDF:

enter image description here

ravi
  • 1,618
-2

Courtesy @musarithmia -- https://tex.stackexchange.com/a/196875/197451

enter image description here

To wrap text in a table column, here are two simple options:

Use tabular and specify one of the columns as a paragraph with p{<width>}. You must specify the width of that column, while the other columns will fit the width of the contents. The wrapped p column will always be left aligned.

\begin{tabular}{p{15mm} c c}

Use one of the other table-making packages. The other answer demonstrates tabularx, which expands the columns to fill a specified width for the whole table.

With tabulary, you specify the width of the whole table, and then you can either choose traditional tabular-style columns with lowercase alignment commands c, l, and r; or you can use uppercase commands to get a wrapping column. This way you can easily create a wrapping column with any alignment.

\begin{tabulary}{\textwidth}{C c c}

Example for beamer:

The first frame uses tabular and the second, tabulary.

\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}
js bibra
  • 21,280
  • 1
    The OP alreeady used p type columns in the code in their question. The problem in OP's case comes from the underscore caracters in "a_long_word_without_space". tabularx or tabulary might on the other hand be a valid advise, however, OP seems to want to have three equally wide left aligned columns. Could you edit your answer to also reflect that instead of just literally copying other users answers? – leandriis Jan 06 '20 at 10:56