如何水平对齐该表格?

如何水平对齐该表格?

有没有办法在这个表中的元素之间设置(动态)间距,而不会弄乱单元格中元素的水平对齐?

\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}

在此处输入图片描述

另一种方法是使用单独的表格线:

\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}

...但结果仍然很草率:

在此处输入图片描述

有人知道对此更清洁的解决方案吗?

答案1

也许这样?使用booktabs\multirow并略有增加\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}

在此处输入图片描述

答案2

multirow这是一个解决方案,使用makecellbooktabs包:

\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} 

在此处输入图片描述

答案3

这不是对您问题的真正回答,因为它没有将内容显示为表格中的表格。不过,考虑其他方式来呈现内容可能会有所帮助。

除了使用表格中的表格之外,您还可以将内容排版为带有注释的代码,例如

\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}

如果您使用该listings包来显示代码,那么您可以使输出更漂亮(例如语法突出显示)。

我个人认为,表内表的方式很耗纸,而且难以阅读。事实上,函数的内置文档R并没有将表放在表中,而是如下所示:

描述

具有 df 自由度和可选非中心参数 ncp 的卡方 (chi^2) 分布的密度、分布函数、分位数函数和随机生成。

用法

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

参数

x, q     vector of quantiles.
p        vector of probabilities.
...

相关内容