[Tex/LaTex] Unable to Wrap Text inside Table in Beamer

beamertablestext;wrap

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?

Best Answer

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

Related Question