2

How can i bottom align the header row only? Is there a way to do it with \rowstyle or something similar as i did to make the whole row \bf?

I would prefer not to use multiple tabulars.

(\documentclass{article}

\usepackage{varwidth}
\usepackage{booktabs} 
\usepackage{array}
\usepackage[aboveskip=1pt,labelfont=bf,labelsep=period,justification=raggedright,singlelinecheck=off]{caption}

\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}


\begin{document}
\begin{table}
        \caption{\newline Weight regulation: long-term clinical trials}
        \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
        \ra{1.3}
        \newcolumntype{M}{>{\begin{varwidth}[t]{3cm}}l<{\end{varwidth}}}    
        \begin{tabular}{@{}$M^M^M@{}}\toprule
            \rowstyle{\bf\raggedbottom}
            First Author \newline Year \newline Country & Bottom &  Bottom \tabularnewline
            \midrule
            \rowstyle{\raggedright}
            This needs to be on top as it is& Also this one& And this
            \tabularnewline
            \bottomrule
        \end{tabular}
        \begin{flushleft} Text in bottom of table.
        \end{flushleft}
        \label{table1}
    \end{table}
\end{document}

EDIT: The text doesn't wrap with the proposed solutions below, but that is not the most important thing in the header row, since \\ or \cr can be used to fix this.

  • Welcome to TeX SX! This needs to be on top as it is means what? On top of what? – Bernard May 15 '16 at 12:27
  • It just needs to be aligned at top as it is already, no need to change that. The only thing i need changed is the vertical alignement in the header row. – Nowak May 15 '16 at 12:30
1

I propose a solution that doesn't require \rowstyle for the first row. The makecell package allows for line breaks in cells, and a common formatting with the \thead, \makecell and a few other commands.

Further, your caption and text at the bottom of the table will use a whole \linewidth, which is probably not what you want. It couldn't be detected because they were not long enough. I suggest using the threeparttable package, which limits theit width to the real table width.

\documentclass{article}

\usepackage{varwidth}
\usepackage{booktabs}
\usepackage{array}
\usepackage[aboveskip=1pt,labelfont=bf,labelsep=period, justification=raggedright,singlelinecheck=off, labelsep=newline, skip=1ex]{caption}
\usepackage{makecell,}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadalign{lb}
\usepackage[flushleft]{threeparttable}

\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}

\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\newcolumntype{M}{>{\varwidth[t]{3cm}}l<{\endvarwidth}}

\begin{document}
\begin{table}
  \begin{threeparttable}
    \caption{Weight regulation: long-term clinical trials. Some more text}
    \ra{1.3}
    \begin{tabular}{@{}$M^M^M@{}}
      \toprule
      %\rowstyle{\bfseries\normalsize\raggedbottom}
      \thead{First Author \\ Year \\ Country} &\thead{Bottom} & \thead{Bottom} \tabularnewline
      \midrule
      \rowstyle{\raggedright}
      This needs to be on top as it is & Also this one & And this \tabularnewline
      \addlinespace
      \midrule[\heavyrulewidth]
    \end{tabular}
    \begin{tablenotes} \item[\hspace*{-\fontdimen2\font}] Text in bottom of table. Text in bottom of table. Text in bottom of table. Text in bottom of table.
    \end{tablenotes}
    \label{table1}
  \end{threeparttable}
\end{table}

\end{document} 

enter image description here

| improve this answer | |
  • Thanks for the solution with the makecell package, it simplifies a lot! And also thanks for suggesting \threeparttable, but my table will exceed the width of the caption when completed. – Nowak May 15 '16 at 22:04
  • Will it also exceed the text at the bottom of the table? – Bernard May 15 '16 at 22:07
  • Great point! But yes, i will need all the space i can get for this one :) But it is very useful indeed, and it will undoubtedly come in handy in the future. – Nowak May 15 '16 at 22:19
  • I worked a little more on my table and i ran in to an issue with this solution. The space-suppressing qualities of @{} gets disrupted by makecell. is there a way that this quality could be restored with adding something in the preample perhaps? See my edited 1st post for clarification. – Nowak May 16 '16 at 11:52
  • I'm not sure I understand well. Do you mean that Bottom should be right-aligned? – Bernard May 16 '16 at 12:23
0

One can just put the desired elements into a \Longstack, though I had to add an extra group, perhaps because of its inclusion in a varwidth environment. To summarize, I added this to the preamble

\usepackage{stackengine}
\setstackEOL{\cr}
\renewcommand\stackalignment{l}

to get left-aligned stacks, with \cr as the row separator. Then, in the tabular, I used this for the entry:

{\Longstack{First Author\cr Year\cr Country}}

Here is the MWE.

\documentclass{article}

\usepackage{varwidth}
\usepackage{booktabs} 
\usepackage{array}
\usepackage[aboveskip=1pt,labelfont=bf,labelsep=period,justification=raggedright,singlelinecheck=off]{caption}
\usepackage{stackengine}
\setstackEOL{\cr}
\renewcommand\stackalignment{l}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}


\begin{document}
\begin{table}
        \caption{\newline Weight regulation: long-term clinical trials}
        \newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
        \ra{1.3}
        \newcolumntype{M}{>{\begin{varwidth}[t]{3cm}}l<{\end{varwidth}}}    
        \begin{tabular}{@{}$M^M^M@{}}\toprule
            \rowstyle{\bf\raggedbottom}
{\Longstack{First Author\cr Year\cr Country}}& Bottom &  Bottom \tabularnewline
            \midrule
            \rowstyle{\raggedright}
            This needs to be on top as it is& Also this one& And this
            \tabularnewline
            \bottomrule
        \end{tabular}
        \begin{flushleft} Text in bottom of table.
        \end{flushleft}
        \label{table1}
    \end{table}
\end{document}

enter image description here

| improve this answer | |
  • Thanks for introducing me to the stackengine! it is a very simple solution to my problem. But i don't know what you mean by "i had to add an extra group"? In the preample? – Nowak May 15 '16 at 22:23
  • @Nowak What I mean is I had to place the \Longstack{...} inside an extra set of braces, as in {\Longstack{...}}. The braces serve as group delimiters. – Steven B. Segletes May 16 '16 at 2:33
  • Thanks for clarifying this. Another thing, i worked a little more on my table and i ran into an issue with the space-suppressing qualities of @{}. It gets disrupted by the stackengine somehow. is there a way this quality could be restored with adding something in the preample perhaps? See my edited 1st post for clarification. – Nowak May 16 '16 at 11:55
  • @Nowak I compiled your edit code and am not sure what the issue is. The table looked good, even with the @{} code present. – Steven B. Segletes May 16 '16 at 12:29
  • I realized that there is no issue. For some reason i wanted the last column in the header row to be right-aligned, but that makes no sense. Sorry for asking, i will remove the issue from my 1st post. – Nowak May 16 '16 at 12:44

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.