Graphics, Figures & TablesDifferent vertical Alignment in Table Columns

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
richi-rich
Posts: 4
Joined: Tue Jul 30, 2013 10:13 am

Different vertical Alignment in Table Columns

Post by richi-rich »

Hy,

I still haven't found a solution for the following problem:

I want to create a table in which specified columns should be vertically centered, but not all. In the first column the text should be aligned at the top.

Within the tabularx environment the m{} column aligns the text along with the top line. Redefining the X column with \renewcommand{\tabularxcolumn}[1]{m{#1}} helps to center the columns, but then the p{} column is ignored (or does something I don't understand :) )

Here is the example:

Code: Select all

\documentclass[a4paper,14pt,BCOR10mm,oneside]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[babel,german=quotes]{csquotes}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage[absolute,overlay]{textpos}
\usepackage{wallpaper}
\usepackage{tabu}
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1.5cm}
\setlength{\marginparwidth}{0.5cm}

\renewcommand{\familydefault}{\sfdefault}
\usepackage{helvet}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}
  \begin{table}[H]
    \begin{tabularx}{\textwidth}{|p{0.1cm}X|m{2.5cm}|m{2.5cm}|m{2.5cm}|}
        \hline
         & \textbf{Fragen} & \centering{Antwort A} & \centering{Antwort B - braucht auch etwas mehr Platz} &
        \centering{Antwort C} \tabularnewline
        \hline
      c. & langer Text langer Text langer Text langer Text langer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
      d. & kurzer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
      e. & kurzer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
      f. & langer Text langer Text langer Text langer Text langer Text langer Text langer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
      \hline
  \end{tabularx}
\end{table}

\end{document} 
Does anybody know how to solve this?

Thanks,
Richie

btw: sorry for the bad english, I hope you still get what i want to tell you :-)
Last edited by richi-rich on Tue Jul 30, 2013 3:03 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Different vertical Alignment in Table Columns

Post by localghost »

Just for information to other users for awareness of possibly already existing solutions. The question has also been posted to MrUnix and GoLaTeX (two German forums, partly with answers). Please read what our administrator thinks about that.
Board Rules wrote:A crossposting is always contra-productive. But there is nothing really against it as long as it is mentioned. This means that a direct link has to be added. So other users who want to help are preserved from double efforts and waste of time.
What about trying it with the tabu package? Where are the difficulties when using its column specifications? Please give a corresponding minimal example if you have already tried it.


Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
richi-rich
Posts: 4
Joined: Tue Jul 30, 2013 10:13 am

Different vertical Alignment in Table Columns

Post by richi-rich »

Hy,

thanks for clarification. I've cross-posted this here because in the german forum my question couldn't be answered.

To the topic:

I've tried it also with the tabu-package, the probleme here is the same, as you can see in this example:

Code: Select all

\documentclass[a4paper,14pt,BCOR10mm,oneside]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[babel,german=quotes]{csquotes}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage[absolute,overlay]{textpos}
\usepackage{wallpaper}
\usepackage{tabu}
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1.5cm}
\setlength{\marginparwidth}{0.5cm}

\renewcommand{\familydefault}{\sfdefault}
\usepackage{helvet}



\begin{document}

\tabulinesep=0.5ex
\begin{table}[H]
    \begin{tabu} spread \linewidth {|p{0.1cm}X[m]|m{2.5cm}|m{2.5cm}|m{2.5cm}|}
        \hline
         & \textbf{Fragen} & \centering{Antwort A} & \centering{Antwort B - braucht auch etwas mehr Platz} &
        \centering{Antwort C} \tabularnewline
        \hline
        c.& langer Text langer Text langer Text langer Text langer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
        d. & kurzer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
        e. & kurzer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
        f. & langer Text langer Text langer Text langer Text langer Text langer Text langer Text & \centering{1} & \centering{2} & \centering{3} \tabularnewline
        \hline
    \end{tabu}
\end{table}


\end{document} 
best regards
Richie
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Different vertical Alignment in Table Columns

Post by cgnieder »

Hi Richie,

Welcome to the LaTeX community. I must confess it's not really clear to me what you want to achieve... However, since you apparently understand German you may be interested in this post on TeXwelt.de/wissen: Wie kann ich innerhalb einer Tabellenzelle vertikal zentrieren?. It explains how the vertical alignment of p{}-, m{}- and b{}-type columns works.

Regards
site moderator & package author
richi-rich
Posts: 4
Joined: Tue Jul 30, 2013 10:13 am

Different vertical Alignment in Table Columns

Post by richi-rich »

Thanks for the link.

What I try to achieve is a table, that looks like this:

Code: Select all


a.xxxxxxxxx|         |         |         |
  xxxxxxxxx|    x    |    x    |    x    |
  xxxxxxxxx|         |         |         |
------------------------------------------
b.xxxxxxxxx|         |         |         |
  xxxxxxxxx|    x    |    x    |    x    |
  xxxxxxxxx|         |         |         |
------------------------------------------
and so on...

best regards,
Richie
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Different vertical Alignment in Table Columns

Post by cgnieder »

Here's an idea: make the labels a., b., ... part of the full cells. Below I use a dummy first column and place the labels with \llap to the left of the first line of the full cells:

Code: Select all

\documentclass[a4paper,14pt,BCOR10mm,oneside]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{tabu}

\renewcommand{\familydefault}{\sfdefault}
\usepackage{helvet}

\newcommand*\tablabel{%
  \stepcounter{tablabel}%
  \leavevmode
  \llap{\makebox[\tablabelwidth]{\thetablabel\hfil}\hspace*{\tablabelsep}}%
}
\newcounter{tablabel}
\renewcommand*\thetablabel{\alph{tablabel}.}
\newlength\tablabelsep
\setlength\tablabelsep{1em}
\newlength\tablabelwidth
\setlength\tablabelwidth{1em}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}
\begin{table}
  \setcounter{tablabel}{2}
  \begin{tabu}{|X[1]X[10l,m]|X[5c,m]|X[5c,m]|X[5c,m]|}
    \hline
      & \textbf{Fragen}
      & Antwort A
      & Antwort B -- braucht auch etwas mehr Platz
      & Antwort C \\
    \hline
      & \tablabel langer Text langer Text langer Text langer Text langer Text
      & 1 & 2 & 3 \\
    \hline
      & \tablabel kurzer Text
      & 1 & 2 & 3 \\
    \hline
      &  \tablabel kurzer Text
      & 1 & 2 & 3 \\
    \hline
      & \tablabel langer Text langer Text langer Text langer Text langer Text
      langer Text langer Text
      & 1 & 2 & 3 \\
    \hline
\end{tabu}
\end{table}

\end{document}
tabelle.png
tabelle.png (25.5 KiB) Viewed 26849 times
Regards
site moderator & package author
richi-rich
Posts: 4
Joined: Tue Jul 30, 2013 10:13 am

Different vertical Alignment in Table Columns

Post by richi-rich »

Hy,

This works well, also with arabic numbers using \arabic instead of \alpha

Code: Select all

\newcounter{tablabel}
\renewcommand*\thetablabel{\arabic{tablabel}.}
\newlength\tablabelsep
\setlength\tablabelsep{1em}
\newlength\tablabelwidth
\setlength\tablabelwidth{1em}
Thanks for the help!

Regards
Richie
vaman
Posts: 17
Joined: Wed Oct 09, 2019 5:51 am

Different vertical Alignment in Table Columns

Post by vaman »

Code: Select all

\documentclass[a4paper,12pt,BCOR10mm,oneside]{scrartcl}
%\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[babel,german=quotes]{csquotes}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage[absolute,overlay]{textpos}
\usepackage{wallpaper}
\usepackage{tabu}
\areaset{17cm}{26cm}
\setlength{\topmargin}{-1.5cm}
\setlength{\marginparwidth}{0.5cm}
\def\bc{\begin{center}}
\def\ec{\end{center}}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{helvet}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}
  \begin{table}[htbh]
    \begin{tabular}{|p{0.1cm}|p{2.5cm}|p{2.5cm}|p{2.5cm}|p{2.5cm}|}
        \hline
       a. & \bc  \textbf{Question} \ec & \bc Answer A \ec & \bc Answer B \ec & \bc Purchased very costly  Place \ec  \bc
         Antwort C \ec \\\hline
        
      c. & longer Text longer Text longer Text longer Text longer Text & \bc 1 \ec & \bc  2 \ec& \bc 3 \ec \\
        \hline
      d. & shorter Text & \bc 1\ec & \bc 2 \ec & \bc 3 \ec \\ 
        \hline
      e. & shorter Text & \bc 1\ec & \bc 2 \ec & \bc 3 \ec \\
        \hline
      f. & longer Text longer Text longer Text longer Text longer Text longer Text longer Text & \bc 1 \ec & \bc 2 \ec & \bc 3 \ec \\
      \hline
  \end{tabular}
\end{table}

\end{document}
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Different vertical Alignment in Table Columns

Post by Ijon Tichy »

@vaman Do you really think, that it makes sense to give an answer to this very old already answered question, that does not provide the wanted vertical alignment of the table cell contents? If you really think so, please note:
  • Option BCOR10mm is deprecated for more than a decade. You shouldn't use it in new answers.
  • \usepackage[utf8]{inputenc} is not needed since LaTeX 2018/04/01.
  • Package tabu is unsupported for more than a decade. It has known issues. It has been broken with new LaTeX releases several times. Currently some of the LaTeX team do keep it alive, but this can change and does not include fixing issues, that are not related to LaTeX kernel changed. → https://github.com/tabu-issues-for-futu ... ainer/tabu
  • Definitions like \bc and \ec are not recommended, because they break the highlight and the syntax checking features of editors.
  • Using \def for definitions (not only, but especially for two letter commands) is dangerous and should be avoided. Users should always use the LaTeX definition commands, e.g., \newcommand, \NewDocumentCommand etc.
So, this is one more ugly answer. I recommend users to ignore it.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Different vertical Alignment in Table Columns

Post by Stefan Kottwitz »

Also I recommend that readers ignore the post by vaman. In addition to what Ijon said: this \bc ... \ec in table cells causes additional vertical space. That's because the center environment is for displaying centered text within surrounding text with a vertical spacing before and after. That's not intended for table cells, especially with that spacing around. \centering would be the choice here, and it should go to the table column definition and not into each cell.

Stefan
LaTeX.org admin
Post Reply