1

It is very annoying to manually break lines within a table that contains lot of text. I have seen several questions (many really old, maybe deprecated) around here regarding setting the width of tables, and wrapping words. However trying different solutions none seems to fulfill all these four requirements: wrap words, align to the left in all columns, use only textwidth for the entire table and preserve table numbering.

EDIT (added packages to code)

I ended up with this code:

\documentclass[12pt,a4paper]{book}
\usepackage{booktabs}
\usepackage{ragged2e}
\setlength\RaggedRightParindent{15pt}
\RaggedRight
\usepackage{pdfsync}

\begin{document}
        \begin{table}[htbp]

      \centering
      \caption{caption goes here }


       \begin{tabular}{p{0.2\linewidth}p{0.8\linewidth}}


        \toprule
        years & description \\
        \midrule
            1954 - 1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.   \\&
        \\
        1954 - 1960     & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est. 
        \\& \\
        1954 - 1960     & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.

           \bottomrule
        \end{tabular} %}

      \label{tab:res}%
    \end{table}%
\end{document}

Which yields a result as follows:

problem

Using the above yields several problems: the text is not aligned to the left, available width is obviously surpassed.

So how can this be solved while making sure all of these four are fulfilled?: wrap words automatically, align to the left in all columns, use only textwidth for the entire table and preserve table numbering

UPDATE

There seems to be an issue with the package pdfsync, since removing that from the code returns a nicely formatted table as follows:

no pdfsync

So now my question is if there is a solution that is compatible with pdfsync?

UPDATE 2

It turns out that I use many packages in a 150 page long document. In that document the table doesn't fit to linewidth as desired even when pdfsync is disabled (it worked fine the first time I compiled after disabling pdfsync, but not afterwards). However when I add all the packages and preamble setup to a completely blank document, and disabling pdfsync the table formats perfectly. So that got me started thinking about caching and stuff like that. Creating a new project that is an exact duplicate of the original one seemed to solve the problem.

  • please fix your code example above to be an example that people can run and see the problem. I do not see how the fragment you post can make the image that you post, in particular the image seems to be ragged left alignment and you show no code to produce that. – David Carlisle Apr 12 '18 at 11:42
  • Fixed, added the relevant packages. – jibo Apr 12 '18 at 12:14
  • 1
    there is no reason to use pdfsync in current (less than ten years or so) tex distributions as synctex is built in to pdftex, luatex and xetex – David Carlisle Apr 12 '18 at 12:18
  • That is good to know. It must have come along with the templates provided by the institution here and not updated with respect to this package. – jibo Apr 12 '18 at 21:01
3

I suggest to use a tabularx:

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}

\begin{table}[htbp]
    \caption{caption goes here}
    \begin{tabularx}{\columnwidth}{@{}lX@{}}
    \toprule
        \multicolumn{1}{c}{years} & \multicolumn{1}{c}{description} \\
        \midrule
        1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\
        \addlinespace
        1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\
        \addlinespace
        1954--1960 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur varius et ex ut efficitur. Phasellus quis commodo diam. Nulla eu aliquet mauris. Nunc dapibus nisi ac lorem pretium aliquet. Sed posuere semper augue vel lacinia. Duis neque diam, ullamcorper in aliquam in, posuere id enim. Cras dui nulla, iaculis quis mauris in, vestibulum maximus nulla. Nulla et enim mollis, ullamcorper dui quis, pretium lacus. Fusce vitae rutrum diam. Ut a odio tortor. Vestibulum non tincidunt est. Etiam sit amet auctor est.\\
        \bottomrule
    \end{tabularx}
    \label{tab:res}%
\end{table}

\end{document}

enter image description here

  • 1
    I'd remove the outer paddings, with @{} at either side and center the headers. And use \columnwidth instead of \linewidth. Oh, and an en-dash between the years. – egreg Apr 12 '18 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.