0

I am trying to use p specification to get text in my table to wrap. I have looked at examples and other questions but can not get it to work. I generated my table with a latex tables generator and I am using some colours so the table code is quite messy. I have tried to put the p-code,

p{40mm},

in the table. More specifically where code colour is specified for columns, as follows,

\begin{table}[H]
\begin{tabular}{
>{\columncolor[HTML]{8E7CC3}} p{40mm} l 
>{\columncolor[HTML]{D9D2E9}} p{40mm} l 
>{\columncolor[HTML]{FFF2CC}} p{40mm} l 
>{\columncolor[HTML]{EA9999}} p{40mm} l}
\cline{1-3}
... more code,

but this just makes the text wrap in one of the rows. Where do I set the column width?

I have provided a working example below (with no attempts of text wrapping).

\documentclass[12pt,a4paper,swedish]{report}

    
\usepackage[table]{xcolor} % For tables (color)
\usepackage{moreverb}                               % List settings
\usepackage{textcomp}                               % Fonts, symbols etc.


\usepackage[T1]{fontenc}                            % Output settings
\usepackage[utf8]{inputenc}                         % Input settings
\usepackage{amsmath}                                % Mathematical expressions (American mathematical society)
\usepackage{amssymb}                                % Mathematical symbols (American mathematical society)
\usepackage{graphicx}                               % Figures

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables


\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=3cm]{geometry}         % Page margin lengths           

\usepackage{float}                                  % Enables object position enforcement using [H]

\usepackage{siunitx} % For units
\usepackage{gensymb} % For certain symbols such as \degree


\usepackage{parskip}                                % Enables vertical spaces correctly 



\begin{document}


\begin{table}[H]
\begin{tabular}{
>{\columncolor[HTML]{8E7CC3}}l 
>{\columncolor[HTML]{D9D2E9}}l 
>{\columncolor[HTML]{FFF2CC}}l 
>{\columncolor[HTML]{EA9999}}l}
\cline{1-3}
\multicolumn{1}{|l|}{\cellcolor[HTML]{8E7CC3}\textbf{Krav}}         & \multicolumn{1}{l|}{\cellcolor[HTML]{D9D2E9}\textbf{Mått}}                      & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}\textbf{Beskrivning}}                                                                             & \textbf{Kravtyp}             \\ \cline{1-3}
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}First thing} & $50-54\degree C$                                                           & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this} & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Second thing}                & $x mAh (5V)$                                                                      & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                                  & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Third thing}              & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                 & Krav                         \\
\cellcolor[HTML]{8E7CC3}Fourth thing                               & \cellcolor[HTML]{D9D2E9}{\color[HTML]{000000} \textgreater{}$100\degree C$} & \cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this                             & \cellcolor[HTML]{EA9999}Krav \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Fifth thing}              & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                                           & Krav                         \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Sixth thing}             & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                              & Önskemål                     \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Seventh thing}     & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}               & Önskemål                     \\
\multicolumn{1}{|l}{\cellcolor[HTML]{8E7CC3}Eighth thing}  & -                                                                               & \multicolumn{1}{l|}{\cellcolor[HTML]{FFF2CC}This is how the thing works and so on. These cells have much text. Kind of like this}                                               & Önskemål                    
\end{tabular}
\end{table}


\end{document}

I am writing in overleaf.

6
  • With the first code snippet you create 8 coulmns: each p{40mm} create one and each l creates one. So remove the ls there. Why do you use \multicolumn{1}{|l}{...} everywhere?
    – dexteritas
    Commented Mar 24, 2023 at 8:35
  • 1
    When you write \multicolumn{1}{l|} in a cell, the specification p{4cm} of that column is locally overwritten in that cell by a columun of type l and, so, the p{4cm} has no longer effect in that cell... Commented Mar 24, 2023 at 8:38
  • Welcome to TeX.SE! Get rid of all your \multicolumns and cellcolors inside the tabular, change l in your column specification to p{4cm} and add | between all columns where you want to have vertical lines. Commented Mar 24, 2023 at 8:41
  • Thank you all very much!It worked just fine when I removed the multicolumns and switched out l for | . Not sure why I specified the cellcolor in both the columns and every cell specifically either... I am not used to creating tabulars from scratch and mostly use generators which is why I also have a hard time debugging.
    – sticksup13
    Commented Mar 24, 2023 at 8:58
  • Is there any way to mark a question as "answered" or "complete"? Perhaps that is taken care of automatically?
    – sticksup13
    Commented Mar 24, 2023 at 9:01

2 Answers 2

2

I realized what I said in my comment and also typesetted the units correctly using siunitx's \qty.

\documentclass[12pt,a4paper,swedish]{report}

    
\usepackage[table]{xcolor} % For tables (color)
\usepackage{moreverb}                               % List settings
\usepackage{textcomp}                               % Fonts, symbols etc.


\usepackage[T1]{fontenc}                            % Output settings
\usepackage[utf8]{inputenc}                         % Input settings
\usepackage{amsmath}                                % Mathematical expressions (American mathematical society)
\usepackage{amssymb}                                % Mathematical symbols (American mathematical society)
\usepackage{graphicx}                               % Figures

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables


\usepackage[top=3cm, bottom=3cm,
            inner=3cm, outer=3cm]{geometry}         % Page margin lengths           

\usepackage{float}                                  % Enables object position enforcement using [H]

\usepackage{siunitx} % For units
\usepackage{gensymb} % For certain symbols such as \degree


\usepackage{parskip}                                % Enables vertical spaces correctly 


\begin{document}


\begin{table}[H]
\begin{tabular}{
|>{\columncolor[HTML]{8E7CC3}}p{3cm}
|>{\columncolor[HTML]{D9D2E9}}p{3cm}
|>{\columncolor[HTML]{FFF2CC}}p{4cm}
|>{\columncolor[HTML]{EA9999}}p{3cm}|}
\cline{1-3}
\textbf{Krav}  & \textbf{Mått}                                                     & \textbf{Beskrivning}                                                                 & \textbf{Kravtyp} \\ \cline{1-3}
First thing    & \qtyrange{50}{54}{\celsius}                                       & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Second thing   & \qty[parse-numbers=false]{x}{\milli\ampere\hour} (\qty{5}{\volt}) & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Third thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Fourth thing   & \qty{>100}{\celsius}                                              & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Fifth thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Krav             \\
Sixth thing    & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål         \\
Seventh thing  & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål         \\
Eighth thing   & -                                                                 & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål                    
\end{tabular}
\end{table}


\end{document}

Result

Is there a reason why you do not want horizontal lines over the last column?

1
  • 2
    Obligatory comment: have a look at the booktabs package which advises to not use vertical rules and shows how to produce professional tables. Commented Mar 24, 2023 at 9:04
1

If you plan to create more complex tables, consider using tblr environment from tabularray. It has much simplified interface. For instance, I have added alternating colours in rows each columns to your table.

You could define global colours and use their names across the table with tints, instead of repeating absolute values in cells.

If the middle column contains long texts, it might look better in a wider column. Additionally, consider loading microtype package to improve texts in narrower environments.

Here's slightly altered table:

enter image description here

\documentclass[12pt,a4paper,swedish]{report}
\usepackage[svgnames]{xcolor} % For tables (color)
\usepackage{amssymb,amsmath}                        % Mathematical expressions (American mathematical society)
\usepackage[
  top=3cm,
  bottom=3cm,
  inner=3cm,
  outer=3cm,
]{geometry}                                         % Page margin lengths           
\usepackage{microtype}
\usepackage{siunitx}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
% \usepackage{textcomp}                               % Fonts, symbols etc.
% \usepackage{moreverb}                               % List settings
% \usepackage{graphicx}                               % Figures
% \usepackage[T1]{fontenc}                            % Output settings
% \usepackage[utf8]{inputenc}                         % Input settings
% \usepackage{float}                                  % Enables object position enforcement using [H]
% \usepackage{gensymb}                                % For certain symbols such as \degree
% \usepackage{parskip}                                % Enables vertical spaces correctly 

\numberwithin{equation}{chapter}                    % Numbering order for equations
\numberwithin{figure}{chapter}                      % Numbering order for figures
\numberwithin{table}{chapter}                       % Numbering order for tables

\colorlet{kravmcol}{BlueViolet!80}
\colorlet{mattcol}{Blue!50}
\colorlet{beskcol}{red!20!yellow!60}
\colorlet{kravtcol}{LightCoral}

\sisetup{
  range-units=single,
  range-phrase={-},
}


\begin{document}
\begin{table}[tbh]
  \footnotesize
  \renewcommand{\arraystretch}{1.5}
  \begin{tblr}{
      colspec = {*2{X[l]} X[3,l] X[l]},
      row{1} = {font=\bfseries, halign=c},
      hline{1,Z} = {wd=0.8pt},
      hline{2} = {wd=0.5pt},
      hline{3-Y} = {wd=0.2pt},
      cell{1}{1} = {bg=kravmcol!80},
      cell{1}{2} = {bg=mattcol!80},
      cell{1}{3} = {bg=beskcol!80},
      cell{1}{4} = {bg=kravtcol!80},
      cell{even[2-Z]}{1} = {bg=kravmcol!50}, cell{odd[2-Z]}{1} = {bg=kravmcol!30},
      cell{even[2-Z]}{2} = {bg=mattcol!50},  cell{odd[2-Z]}{2} = {bg=mattcol!30},
      cell{even[2-Z]}{3} = {bg=beskcol!50},  cell{odd[2-Z]}{3} = {bg=beskcol!30},
      cell{even[2-Z]}{4} = {bg=kravtcol!50}, cell{odd[2-Z]}{4} = {bg=kravtcol!30},
    }
    Krav          & Mått
    & Beskrivning                                                                          & Kravtyp \\
    First thing   & $\qtyrange{50}{54}{\celsius}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Second thing  & $\mathop{x} \unit{\mA}(5)\unit{\hour}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Third thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Fourth thing  & $> 100\unit{\celsius}$
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Fifth thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Krav \\
    Sixth thing   & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål \\
    Seventh thing & -
    & This is how the thing works and so on. These cells have much text. Kind of like this & Önskemål \\
    Eighth thing  & -
    & This is how the thing works and so on. These cells have much text. Kind of like      & Önskemål \\
  \end{tblr}
\end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .