TransWikia.com

Unable to Wrap Text inside Table in Beamer

TeX - LaTeX Asked by Ravi Joshi on April 15, 2021

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.3linewidth}
  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?

2 Answers

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}

Answered by js bibra on April 15, 2021

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.3linewidth}
  % https://tex.stackexchange.com/a/9938
  renewcommand_{textunderscoreallowbreak}
  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_{textunderscoreallowbreak}. This workaround is taken from here

Below is a screenshot of the generated PDF:

enter image description here

Answered by Ravi Joshi on April 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP