Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to have a set (dynamic) spacing between the elements in this table without messing up horizontal alignment of the elements in the cell ?

\documentclass{ltxdoc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage{ragged2e}
\usepackage{array}

\newcommand*{\tabsep}{3ex}
\newcommand*{\TS}{\noalign{\vskip\tabsep}\pagebreak[1]}

\begin{document}

\begin{longtable}[h]{l >{\RaggedRight}m{5cm} >{\RaggedRight}m{5cm} c c} 
\textcolor{blue}{\textbf{R}} code & Syntax & Gebruik & Is vector & Link
\\*\TS
\hline
\TS
\endhead
\texttt{pchisq()} & \begin{tabular}{l}\texttt{\textcolor{blue}{pchisq(}\textcolor{cyan}{<getal>},} \\*\TS \texttt{\textcolor{green}{df=<waarde>}\textcolor{blue}{)}} \\*\TS \texttt{\textcolor{green}{col=<getal>},} \\*\TS \texttt{\textcolor{red}{lwd=<getal>}} \end{tabular} & \begin{tabular}{>{\RaggedRight}m{5cm}}Calculate P-value in a chi square test \\*\TS  degrees of freedom \\*\TS This code doesn't make sense, but neither does the vertical table alignment \\*\TS Random line width option \end{tabular} & $\surd$ & {$\leftarrow$} \\ 
\end{longtable}
\end{document}

enter image description here

An alternative way to do it would be to use seperate table lines:

\documentclass{ltxdoc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage{ragged2e}
\usepackage{array}

\newcommand*{\tabsep}{3ex}
\newcommand*{\TS}{\noalign{\vskip\tabsep}\pagebreak[1]}

\begin{document}

\begin{longtable}[h]{l >{\RaggedRight}m{5cm} >{\RaggedRight}m{5cm} c c} 
\textcolor{blue}{\textbf{R}} code & Syntax & Gebruik & Is vector & Link
\\*\TS
\hline
\TS
\endhead
& \texttt{\textcolor{blue}{pchisq(}\textcolor{cyan}{<getal>},} & Calculate P-value in a chi square test \\
\\*\TS
& \texttt{\textcolor{green}{df=<waarde>}\textcolor{blue}{)}} & degrees of freedom \\
\texttt{pchisq()} & & & $\surd$ & $\leftarrow$ \\
& \texttt{\textcolor{green}{col=<getal>},} &  This code doesn't make sense, but neither does the vertical table alignment \\
\\*\TS
& \texttt{\textcolor{red}{lwd=<getal>}} & Random line width option \\
\end{longtable}
\end{document}

...but the result is still sloppy:

enter image description here

Does anybody know a cleaner solution to this?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

Perhaps this? Using booktabs, \multirow and increasing a little \arraystretch:

\documentclass{ltxdoc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage{ragged2e}
\usepackage{array}
\usepackage{booktabs}
\usepackage{multirow}
\newcommand*{\tabsep}{3ex}
\newcommand*{\TS}{\noalign{\vskip\tabsep}\pagebreak[1]}

\begin{document}

{
\renewcommand\arraystretch{1.3}
\begin{longtable}{l >{\RaggedRight}m{5cm} >{\RaggedRight}m{5cm} c c} 
\textcolor{blue}{\textbf{R}} code & Syntax & Gebruik & Is vector & Link \\
\toprule
\endhead
\multirow{6}{*}{\texttt{pchisq()}} 
  & \texttt{\textcolor{blue}{pchisq(}\textcolor{cyan}{<getal>},} 
  & Calculate P-value in a chi square test 
  & \multirow{6}{*}{$\surd$} 
  & \multirow{6}{*}{$\leftarrow$} 
\\
& \texttt{\textcolor{green}{df=<waarde>}\textcolor{blue}{)}} 
  & degrees of freedom 
\\
& \texttt{\textcolor{green}{col=<getal>},} 
  & This code doesn't make sense, but neither does the vertical table alignment 
\\
& \texttt{\textcolor{red}{lwd=<getal>}} 
  & Random line width option 
\\
\end{longtable}
}

\end{document}

enter image description here

share|improve this answer
    
How robust is this arraystretch solution? Could I make it a variable one on a cell-by-cell basis, or would I have to declare seperate longtables for that? –  1010011010 May 7 at 0:19
    
@1010011010 You can add space at any line break by using \\[1cm] or other appropriate distance. –  cslstr May 7 at 0:21
    
@1010011010 \arraystretch is a LaTeX kernel command which affects the line spacing for every row on a tabular (the default value is 1); the extra pair of braces in my code keep its redefinition local to the particular longtable. To change spacing between some particular rows, follow cslstr's advise and use \\[<length>]. –  Gonzalo Medina May 7 at 0:26
    
@1010011010 or use \addlinespace[<length>] after ` \\ ` if you are using booktabs. –  Gonzalo Medina May 7 at 0:27
    
Thanks for the extensive/in-depth explanation. I'll have a look at that local solution as well because some cells have 4 rows while others have 7, etc. –  1010011010 May 7 at 0:30

Here is a solution, using the multirow, makecell and booktabs packages:

\documentclass{ltxdoc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage{ragged2e}
\usepackage{array, multirow, booktabs}
\usepackage{makecell}
%\newcommand*{\tabsep}{3ex}
%\newcommand*{\TS}{\noalign{\vskip\tabsep}\pagebreak[1]}
\renewcommand\cellalign{{m{5cm}}c}
\renewcommand\cellgape{\Gape[0.6 ex]}
\renewcommand\theadalign{lc}
\renewcommand\theadfont{\bfseries}

\begin{document}

\begin{longtable}[!h]{l *{2}{>{\RaggedRight}m{5cm}}*{2}{c}}%
\thead{\textcolor{blue}{\textbf{R}} code} & \thead{Syntax} & \thead{Gebruik} & \thead{Is vector }& \thead{Link}
\\
\toprule
\addlinespace
%\TS
\endhead
\multirow{7}{*}{\texttt{pchisq()}}
&  \texttt{\textcolor{blue}{pchisq(}\textcolor{cyan}{<getal>},} & \makecell{Calculate P-value in a chi square test}
& \multirow{7}{*}{$\surd$} & \multirow{7}{*}{$\leftarrow$}\\%
&\texttt{\textcolor{green}{df=<waarde>}\textcolor{blue}{)}} & \makecell{ degrees of freedom} \\
& \texttt{\textcolor{green}{col=<getal>},} &  \makecell*{This code doesn't make sense, but neither does the vertical table alignment} &  \ \\
&  \texttt{\textcolor{red}{lwd=<getal>}} & \makecell*{Random line width option} \\
\end{longtable}

\end{document} 

enter image description here

share|improve this answer

This is not a real answer to your question, in that it does not display the content as a table within a table. Nevertheless it may be useful to consider alternative ways to present the content.

Instead of using a table within a table, you could typeset the content as code with comments, e.g.

\begin{verbatim}
pchisq(<getal>, % calculate p-value in a chi square test
df =<waarde>,   % degrees of freedom
col=<getal>,    % This code doesn't make sense,
                % but neither does the vertical table alignment
lwd =<getal>)   % Random line width option
\end{verbatim}

If you were to use the listings package to display code, then you could make the output more pretty (e.g. syntax highlighting).

Personally, I think the table within a table approach consumes a lot of paper, and is hard to read. In fact, the built-in documentation for R functions does not put a table within a table, but looks like the following:

Description

Density, distribution function, quantile function and random generation for the chi-squared (chi^2) distribution with df degrees of freedom and optional non-centrality parameter ncp.

Usage

dchisq(x, df, ncp = 0, log = FALSE)
...

Arguments

x, q     vector of quantiles.
p        vector of probabilities.
...
share|improve this answer
    
Thanks for the suggestion, yes I included this presentation as well in the full document. I like to present a variety of options so students can learn in their own way and indeed, listings is one of them, but a table-format is another. A small additional edit is that I don't intend to use the end product PDF as a physical paper, but rather an interactive PDF document including links and pop-up hints (probably JS based). –  1010011010 May 7 at 0:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.