1

I am trying to get a text left aligned in a table that automatically wraps text at a specified, varying column width. Without: LaTex adding extra spaces between words to reach the other side (right side) of the column.

My 2 main approaches consist of using the tabular and tabularx environment but both approaches end up spreading the words out from left to right except for the last line.

An example of the desired output: Excel does not add the extra spaces in a left alignment with a wrapped text

MWE Tabular environment:

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}


\section{Introduction}
    test
    \begin{table}[]
    %\begin{tabular}{|l|l|l|l|l|l|l|}
    \hspace*{-11.50em}
    \begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}
    \hline
    Week2Lecture2 ANN: Perceptrons, MLP and Backpropagation &   1. 1.a. Neural Networks Supervised  &   2. 1.b. Neural Networks UnSupervised    &   9. 5.a. Notation    &       &       &       \\ \hline
    W2L3 ANN: Training and building MLPs    &   1. 1.a. Neural Networks Supervised  &   2. 1.b. Neural Networks UnSupervised    &   12. 5.d Gradient descent    &   13. 5.e Training Neural Networks    &   10. 5.b. Model Architecture &   11. 5.c Loss Function   \\ \hline
    W3L4 Reinforcement Learning &   4. 2.a. Reinforcement Learning  &       &       &       &       &       \\ \hline
    W3L5 Genetic Algorithms &   3. 1.c Evolutionairy Computing  &   5. 3.a. Evolutionairy computing &       &       &       &       \\ \hline
    W4L6 Swarm Intelligence &   6. 3.b. Swarm Intelligence  &       &       &       &       &       \\ \hline
    W4L7 CI Math and Principles Overview    &   9. 5.a. Notation    &   7. 3.c Bayesian Reasoning   &       &       &       &       \\ \hline
    W5L8 ANN: Unsupervised  &   2. 1.b. Neural Networks UnSupervised    &       &       &       &       &       \\ \hline
    W5L9 ANN: Deep/Recurrent and Gradient Descent   &   12. 5.d Gradient descent    &   4. 2.a. Reinforcement Learning  &   11. 5.c Loss Function   &       &       &       \\ \hline
    W6L10 Evolutionary Strategies, Genetic Programming  &   5. 3.a. Evolutionairy computing &   3. 1.c Evolutionairy Computing  &       &       &       &       \\ \hline
    W6L11 Mixed CI strategies   &   All &       &       &       &       &       \\ \hline
    W6L12 (Werkcollege) Swarm routing   &   6. 3.b. Swarm Intelligence  &       &       &       &       &       \\ \hline
    \end{tabular}
    \end{table}
\end{document}

Yielding: This is the compilation of the tabular approach As you can see, for example the words "Perceptrons, MLP" in cell 1,1 get stretched out.

MWE Tabularx environment:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{caption}

\begin{document}


\section{Introduction}
    test


\begin{table}[htbp]
    \caption{attempt 11 fix column width}
    \hspace*{-13.2em}
    %\begin{tabularx}{1.7\columnwidth}{@{}|l|X@{}|}
    \begin{tabularx}{1.6\columnwidth}{|p{3cm}|p{3cm}|}
    \toprule
        \multicolumn{1}{c}{years} & \multicolumn{1}{c}{description} \\
        \midrule 
        \hline
        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.\\ \toprule %\hline %\addlinespace
        \bottomrule
        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.\\ \hline \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.\\ \hline \addlinespace
        \bottomrule
    \end{tabularx}
    \label{tab:res}%
\end{table}

\end{document}

Yielding: enter image description here

Which also spreads the words. Any suggestions on how to prevent the spreading of those words, whilst keeping the same words per line, with the fixed column width and the same word breaks would be much appreciated!

1

Indead as @leandriis indicated, the solution was explained in the first link. For completeness applied to this problem it becomes:

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}




\section{Introduction}
    test
    \begin{table}[]
    %\begin{tabular}{|l|l|l|l|l|l|l|}
    \hspace*{-11.50em}
    %\begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}
    \begin{tabular}{| L{3cm} |L{3cm}|L{2.5cm}|L{2cm}|L{2.cm}|L{2.cm}|L{2.cm}|}
    \hline
    Week2Lecture2 ANN: Perceptrons, MLP and Backpropagation &   1. 1.a. Neural Networks Supervised  &   2. 1.b. Neural Networks UnSupervised    &   9. 5.a. Notation    &       &       &       \\ \hline
    W2L3 ANN: Training and building MLPs    &   1. 1.a. Neural Networks Supervised  &   2. 1.b. Neural Networks UnSupervised    &   12. 5.d Gradient descent    &   13. 5.e Training Neural Networks    &   10. 5.b. Model Architecture &   11. 5.c Loss Function   \\ \hline
    W3L4 Reinforcement Learning &   4. 2.a. Reinforcement Learning  &       &       &       &       &       \\ \hline
    W3L5 Genetic Algorithms &   3. 1.c Evolutionairy Computing  &   5. 3.a. Evolutionairy computing &       &       &       &       \\ \hline
    W4L6 Swarm Intelligence &   6. 3.b. Swarm Intelligence  &       &       &       &       &       \\ \hline
    W4L7 CI Math and Principles Overview    &   9. 5.a. Notation    &   7. 3.c Bayesian Reasoning   &       &       &       &       \\ \hline
    W5L8 ANN: Unsupervised  &   2. 1.b. Neural Networks UnSupervised    &       &       &       &       &       \\ \hline
    W5L9 ANN: Deep/Recurrent and Gradient Descent   &   12. 5.d Gradient descent    &   4. 2.a. Reinforcement Learning  &   11. 5.c Loss Function   &       &       &       \\ \hline
    W6L10 Evolutionary Strategies, Genetic Programming  &   5. 3.a. Evolutionairy computing &   3. 1.c Evolutionairy Computing  &       &       &       &       \\ \hline
    W6L11 Mixed CI strategies   &   All &       &       &       &       &       \\ \hline
    W6L12 (Werkcollege) Swarm routing   &   6. 3.b. Swarm Intelligence  &       &       &       &       &       \\ \hline
    \end{tabular}
    \end{table}

\end{document}

Yielding:

enter image description here

The essence was in the line:

\begin{tabular}{|p{3cm}|p{3cm}|p{2.5cm}|p{2cm}|p{2.cm}|p{2.cm}|p{2.cm}|}

Which had to change to:

\begin{tabular}{| L{3cm} |L{3cm}|L{2.5cm}|L{2cm}|L{2.cm}|L{2.cm}|L{2.cm}|}

Whilst adding the described new column types and array package.

  • 2
    If one wants to allow for some hyphenation (whenever possible), one can load ragged2e and code \newcolumntype{L}[1]{>{\RaggedRight\arraybackslash\hspace{0pt}}m{#1}}. – Bernard Sep 13 '18 at 17:32

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.