3

I have troubles with the correct aligning of my table cells. I want to get a look like in the lower picture below. The caption should be no matter for this MWE (I know how to modify this using the caption package...).

  • I don't know how to get corresponding items in the same line if they belong to the same cell (see picture) und how to align all cells on the left top

My result so far is: enter image description here

But I want to have it aligned like this: enter image description here

Here is my code that produces the output in the upper picture (note that the text is German and differs from the example in the lower picture):

\documentclass{article}
\usepackage{ngerman}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pbox}
\usepackage{mhchem}
\usepackage{caption}


\begin{document}

%%%
\newcommand*{\bacterium}[2]{\textit{#1}~\textit{#2}}
\newcommand*{\bacteriumsub}[3]{\textit{#1}~\textit{#2}~ssp.~\textit{#3}}
\newcommand*{\staphk}[1]{\textit{St}.~\textit{#1}}
\newcommand*{\lactok}[1]{\textit{Lb}.~\textit{#1}}
%%%%

\begin{table}[!htbp]
\small
\caption[Title]{Titel}
\centering
\begin{tabular}{llll}
\toprule
Gruppe & Spezies & \pbox{3cm}{metabolische\\ Aktivität} & \pbox{5cm}    {Ziel}\\
\midrule
     % first row
     \pbox{3cm}{Milchsäure-\\bakterien} & \pbox{5cm}{\lactok{plantarum}\\ \lactok{pentosus}\\ \lactok{sakei}\\ \lactok{curvatus}\\ \bacterium{P.}{acidilactici}\\\bacterium{P.}{pentosaceus}} & \pbox{4cm}{Bildung von\\ Milchsäure} & \pbox{5cm}{Inhiberung v. Pathogenen\\ u. Verderbsorganismen,\\ Beschleunigung d. Umrötung,\\ Textur,\\ Aroma} \\
     \\
     % second row
     \pbox{3cm}{Katalase-\\positive\\ Kokken} & \pbox{5cm}{\staphk{xylosus}\\ \bacteriumsub{St.}{carnosus}{carnosus}\\ \bacteriumsub{St.}{carnosus}{utilis}\\ \bacterium{Kocuria}{varians}} & \pbox{4cm}{Nitratreduktion,\\ Nitritreduktion,\\ Verbrauch von \ce{O2},\\ Dekompensation v.\\ Peroxiden} & \pbox{5cm}{Umrötung u.\\ Farbstabilisierung,\\Modulation von\\Aroma u. Geschmack,\\ Minimierung v.\\ Ranzidität} \\    
     % third row 
     \\
     \pbox{3cm}{Hefen} & \pbox{5cm}{\bacterium{Deb.}{hansenii}\\ \bacterium{Candida}{famata}} \\ 
     % fourth row
     \\
     \pbox{3cm}{Schimmel-\\pilze} & \pbox{5cm}{\bacterium{Pen.}{nalgiovense}\\ \bacterium{Pen.}{chrysogenum}} \\
\bottomrule
\end{tabular}
\end{table}


\end{document}
  • 2
    Use p-columns instead of pboxes. – Johannes_B Dec 3 '15 at 13:54
  • Are capital letters rationed where you are? Seems draconian.... – cfr Dec 3 '15 at 14:20
  • 1
    That is, \begin{tabular}{p{3cm}p{3cm}p{5cm}p{3cm}} or whatever. – cfr Dec 3 '15 at 14:21
3

There are a number of improvements for your code in addition to the questions raised by yourself:

  1. Don't do the linebreaks inside words explicitly (if there is another solution possible). Here, it suffices to tell LaTeX where a good place for a linebreak is by inserting \- at the respective positions in the words.
  2. As mentioned in the comment by Johannes_B, it is better to define the width of a whole column rather than that of every cell. So you should use p{width} instead of l as specifier for the columns.
  3. Now to your first problem: How to align the cells on the left top? If you do not want to insert manual linebreaks, this is automatically done. If you wish to insert linebreaks manually, you can put the content of a cell into a minipage environment and pass [t] as a parameter to it such that it is aligned at the top.
  4. Your last problem (if I understood that correctly) is to align parts of the cell contents with each other. To this end, you can use the multirow package defining the command with the same name. By this, you can declare that a certain cell occupies more than one row. Thus, you can use certain rows to align that parts of the cells that you would like to relate to each other.

The result of applying these things to your code is:

\documentclass{article}
\usepackage{ngerman}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pbox}
\usepackage[version=4]{mhchem}
\usepackage{caption}
\usepackage{multirow}

\begin{document}

%%%
\newcommand*{\bacterium}[2]{\textit{#1}~\textit{#2}}
\newcommand*{\bacteriumsub}[3]{\textit{#1}~\textit{#2}~ssp.~\textit{#3}}
\newcommand*{\staphk}[1]{\textit{St}.~\textit{#1}}
\newcommand*{\lactok}[1]{\textit{Lb}.~\textit{#1}}
%%%%

\begin{table}[!htbp]
\small
\caption[Title]{Titel}
\centering
\begin{tabular}{p{2cm}p{4cm}p{3.5cm}p{4.5cm}}
\toprule
Gruppe & Spezies & metabolische Aktivität & Ziel\\
\midrule
     % first row
     Milchsäure\-bakterien & \begin{minipage}[t]{4cm}\lactok{plantarum}\\\lactok{pentosus}\\\lactok{sakei}\\\lactok{curvatus}\\\bacterium{P.}{acidilactici}\\\bacterium{P.}{pentosaceus}\end{minipage} & Bildung von Milchsäure & \begin{minipage}[t]{4.5cm}Inhiberung v.\ Pathogenen u.\ Verderbsorganismen,\\ Beschleunigung d.\ Umrötung,\\ Textur,\\ Aroma\end{minipage} \\
     \\
     % second row
     \multirow{3}{2cm}{Katalase\-positive Kokken} & \multirow{3}{4cm}{\begin{minipage}[t]{4cm}\staphk{xylosus}\\\bacteriumsub{St.}{carnosus}{carnosus}\\\bacteriumsub{St.}{carnosus}{utilis}\\\bacterium{Kocuria}{varians}\end{minipage}} & Nitritreduktion, & Umrötung u.\ Farbstabilisierung, \\    
     &&  Verbrauch von \ce{O2}, & Modulation von Aroma u.\ Geschmack, \\
     &&  Dekompensation v.\ Peroxiden & Minimierung v.\ Ranzidität \\
     % third row 
     \\
     Hefen & \begin{minipage}[t]{4cm}\bacterium{Deb.}{hansenii}\\\bacterium{Candida}{famata}\end{minipage} \\ 
     % fourth row
     \\
     Schimmel\-pilze & \begin{minipage}[t]{4cm}\bacterium{Pen.}{nalgiovense}\\\bacterium{Pen.}{chrysogenum}\end{minipage} \\
\bottomrule
\end{tabular}
\end{table}


\end{document}

That yields the following output:

enter image description here

  • thank you very much for the detailed explanation. it helped me very much! – Jaska Dec 6 '15 at 14:47
2

One more (minimalistic) alternative. For it I add makecell (used for column heads and extra spaces between main rows) and ragged2e (for smart ragged text in call table's cells) packages, remove \newcommands for itshape text in second column, remove pbox package and its use in table and use normal size fonts (instead small). Since table seems to be text width wide, I use the tabularx instead table. For it it is defined new column type, which enable simple typing of table:

\documentclass{article}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
%    \usepackage{ngerman}
    \usepackage{booktabs,makecell,tabularx}
%\renewcommand\theadfont{\bfseries\upshape}
\frenchspacing
    \usepackage{ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash\let\\\newline}X}
    \usepackage[version=4]{mhchem}
    \usepackage{caption}
%%%%
\usepackage[margin=25mm,showframe]{geometry}%to see page layout

    \begin{document}
    \null\bigskip
\begin{table}[htbp]
\caption[Title]{Titel}
    \begin{tabularx}{\textwidth}{>{\hsize=.4\hsize}L
                     >{\hsize=1.1\hsize\itshape}L
                     >{\hsize=1.3\hsize}L
                     >{\hsize=1.2\hsize}L
                     @{}}
    \toprule
\textbf{Gruppe}
    &   \textb{Spezies} 
        &   \textb{Metabolische Aktivität}
            &   \textb{Ziel}                                \tabularnewline
    \midrule
% first row
Milchsäure\-bakterien
    &   Lb. plantarum               \\
        Lb. pentosus                \\
        Lb. sakei                   \\
        Lb. curvatus                \\
        P.  acidilactici            \\
        P. pentosaceus
        &   Bildung von Milchsäure
            &   Inhiberung v. Pathogenen u. Verderbsorganismen,
                Beschleunigung d. Umrötung, Textur, Aroma   \tabularnewline
    \addlinespace
% second row
Katalase\-positive Kokken
    &   St. xylosus                 \\
        St. carnosus ssp. carnosus  \\
        St. carnosus ssp. utilis
        &   Nitritreduktion, Umrötung u. Farbstabilisierung, 
            Verbrauch von \ce{O2},
            &   Modulation von Aroma u.\ Geschmack,         \tabularnewline
    &   Kocuria varians
        &   Dekompensation v.\ Peroxiden
            &   Minimierung v.\ Ranzidität                  \tabularnewline
    \addlinespace
% third row 
Hefen
    &   Deb. hansenii               \\
        Candida famata                                      \tabularnewline
   \addlinespace
% fourth row
Schimmel\-pilze
    &   Pen. nalgiovense            \\
        Pen. chrysogenum                                    \tabularnewline
    \bottomrule
\end{tabularx}
\end{table}
    \end{document}

Package ngerman is comment since I haven't installed it. With geometry with option showframe is defined page layout (about it I guessing from sum of column/pbox widths) and showed page layout. This MWE gives:

enter image description here

Edit: according to Mico comments i make some changes to original answer: (i) column heads change from centered to left align (with replace thead{...} with textbf{...}); (ii) the width of tabularx is text width, so here is nothing to centering, so I erase redundant instruction \centering; and (iii) for correct inter-words spacing I add \franchspacing into MWE preamble.

Some additional explanations:

  • by use of L (X width smart ragged right text) type of column the use of \pbox{<width>}{...} as well mini pages in column is surplus. Those columns enabled multi lines cells "per se", their width is calculated by tabularx and in this case their widths ratio is manually tuned (for better looking table)
  • since minimal heights of rows is enough big that more lines in the first column cells doesn't interfere them. Consequently the use of multirow cells is not needed anymore.

By all this measures the table code is lean and concise (to my opinion). With the way how it is set in editor, it is (to my opinion again) easy to see and understand the table structure or upgrade it latter width new data if necessary.

  • @Mico, thank you very much for comments! For the (i): this is matter of personal taste /habits in some places, anyway, I will change this as you suggest. for (ii) I simply overlooked this , however for (iii) I wasn't aware. I learn something new too. – Zarko Dec 6 '15 at 16:18
0

Some suggestions:

  • Use a tabularx environment instead of a tabular environment, and set the width of the tabularx environment to \textwidth, i.e., the width of the text block.

  • The contents of the second column should probably never be line-broken; hence keep assigning the l column type to the second column. The contents of columns 1, 3, and 4, in contrast, can (and should) be line-broken: assign a modified form of the X column type to them, where the modification is to allow hyphenation in otherwise right-aligned text.

  • Remove all interior linebreaks from the contents of columns 1, 3, and 4. For some of the words in column 1, provide \- discretionary hyphen break information, e.g., Milchsäure\-bakterien, Katalase\-positive, and Schimmel\-pilze.

  • To simplify the typesetting of the contents of column 2, I propose using a helper macro, called \columnlist in the code below.

  • The ngerman package is deprecated; consider using the babel package with the option ngerman instead.

enter image description here

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[a4paper,margin=1in]{geometry} % set page parameters
\usepackage{tabularx,ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}

\usepackage{booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[version=4]{mhchem}
\usepackage{caption}

\newcommand\columnlist[1]{%
   \begin{tabular}[t]{@{}l@{}}
   #1
   \end{tabular}}
\begin{document}

\newcommand*{\bacterium}[2]{\textit{#1}~\textit{#2}}
\newcommand*{\bacteriumsub}[3]{\textit{#1}~\textit{#2}~ssp.~\textit{#3}}
\newcommand*{\staphk}[1]{\textit{St}.~\textit{#1}}
\newcommand*{\lactok}[1]{\textit{Lb}.~\textit{#1}}

\begin{table}[!htbp]
\caption[Title]{Titel}

% Note: Sum of 3 `\hsize` directives = 3 = # of columns of type "L"
\begin{tabularx}{\textwidth}{@{}
    >{\hsize=0.5\hsize}L
    l
    >{\hsize=1.25\hsize}L
    >{\hsize=1.25\hsize}L @{}}
\toprule
Gruppe & Spezies & Metabolische Aktivität & Ziel\\
\midrule
Milchsäure\-bakterien & 
\columnlist{%
  \lactok{plantarum}\\ 
  \lactok{pentosus}\\ 
  \lactok{sakei}\\ 
  \lactok{curvatus}\\ 
  \bacterium{P.}{acidilactici}\\
  \bacterium{P.}{pentosaceus}} & 
  Bildung von Milchsäure & 
  Inhibierung v. Pathogenen u. Verderbsorganismen, 
  Beschleunigung d. Umrötung, 
  Textur, Aroma \\
\addlinespace
% second row
Katalase\-positive Kokken & 
\columnlist{%
  \staphk{xylosus}\\ 
  \bacteriumsub{St.}{carnosus}{carnosus}\\ 
  \bacteriumsub{St.}{carnosus}{utilis}\\ 
  \bacterium{Kocuria}{varians}} & 
  Nitratreduktion, Nitritreduktion,  
  Verbrauch von \ce{O2}, 
  Dekompensation v. Peroxiden & 
  Umrötung u. Farbstabili\-sie\-rung, Modulation von Aroma u. Geschmack, Minimierung v. Ranzidität \\   
\addlinespace 
% third row
Hefen &
\columnlist{%
  \bacterium{Deb.}{hansenii}\\ 
  \bacterium{Candida}{famata}} \\
\addlinespace
% fourth row
Schimmel\-pilze & 
\columnlist{%
  \bacterium{Pen.}{nalgiovense}\\ 
  \bacterium{Pen.}{chrysogenum}}\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

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.