长表标题中的垂直对齐

长表标题中的垂直对齐

我遇到了一个问题,我的 longtable 的标题单元格未正确对齐。第一个标题中的文本似乎向下对齐(或者可能在它前面有一个神秘的换行符),而第二个和第三个单元格中的文本似乎向上对齐(或者在它后面有一个神秘的换行符)。

\documentclass{report}

\usepackage{longtable}

% Define an easy way to make line breaks within tables
\newcommand{\specialcell}[2][t]{
  \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}

%===----- Functions for indexing and typesetting -----=====
\newcommand{\fun}[1]{\texttt{#1}\index{#1}}
\newcommand{\FUN}[1]{\texttt{#1}\index{#1|textbf}}
\newcommand{\code}[1]{\texttt{#1}}

\begin{document}
\SweaveOpts{concordance=TRUE}


\begin{longtable}{p{0.12\textwidth} | p{0.50\textwidth} | p{0.38\textwidth}}
\label{tab:basicfunctions} 

\textbf{Function} & \textbf{What it does} & \textbf{Example use} \\ \hline
\endhead

\fun{:} & A handy little function that generates a sequence of numbers, counting by 1, backwards or forwards. & \specialcell{\code{0:18} \\ \code{48:-8}} \\
\fun{?} & Finds help files for a specific function, package or dataset. & \code{?max}\\
\fun{??} & Finds help files that match a given topic & \code{??correlation}\\
\fun{c} & Combines input to form a vector. Input must be of the same data type (e.g., only letters, only numbers, etc.) & \specialcell{\code{c(1,2,5,9)}\\ \code{c("imagine","that")}} \\
\fun{cor} & Returns the correlation between two vectors, or between the columns of a matrix. & \code{cor(c(1,3,5,7),c(4,2,8,6))}\\ 
\fun{cumsum} & Calculates the cumulative sum of its input. For vectors, it returns a vector. For matrices, it adds down a column first, then continues with the next column. The result is returned as a vector. & \code{cumsum(c(5,10,2,9))}\\
\fun{diff} & Calculates the sequential difference between a series of numbers. & \code{diff(c(2,4,5,8))}\\
\fun{dir} & Lists the files in a directory or folder. Default is the current directory, but other locations can be provided. The same as \fun{list.files}. & \specialcell{\code{dir()}\\\code{dir("C:/")} (Windows) \\ \code{dir("/Users/")} (Mac)} \\
\fun{example} & Runs the example code at the bottom of the help page for a given function. & \code{example(cumsum)} \\
\fun{head} & Shows the first six elements of a vector or the first six rows of a matrix. & \code{head(letters)} \\
\FUN{intersect} & Returns the matching elements of two vectors. & \code{intersect(c(2,4),c(1,2))} \\
\fun{length} & Returns the length of a vector. For a matrix, it returns the total number of elements. & \code{length(LETTERS)} \\
\fun{list.files} & The same as \code{dir}; provides a list of files in a directory. & \specialcell{\code{list.files()}\\ \code{list.files("C:/")} (Windows) \\ \code{list.files("/Users/")} (Mac)} \\
\fun{ls} & Gives the names of all the objects currently in working memory. & \code{ls()}\\
\fun{max} & Returns the largest value in its input. & \code{max(1:100)} \\
\fun{mean} & Returns the arithmetic average of its input. & \code{mean(c(1,2,3,10))} \\
\fun{median} & Returns the middle value of its input. & \code{median(c(1,2,3,10)} \\
\fun{min} & Returns the smallest value in its input. & \code{min(1:100)}\\
\fun{nchar} & Gives the number of characters in each element of a vector containing strings of text. & \code{nchar(c("imagine","that"))}\\
\fun{order} & Returns the order of elements in a vector. Note that this does not sort the vector. Instead, it tells you how the elements of the vector would need to be rearranged to sort them. & \code{order(c(9,5,8,2))} \\    
\fun{rep} & Repeats its input a given number of times. Can be used on numbers, characters, and other kinds of data. & \specialcell{\code{rep(1,5)} \\ \code{rep(1:3,5)} \\ \code{rep("okay",10)}} \\
\fun{rev} & Reverses the contents of its input, and returns them as a vector. & \code{rev(c("is","it","cool"))} \\
\end{longtable}


\end{document}

结果如下: 图像中的一些标题单元格底部对齐,其他标题单元格顶部对齐

在此先感谢您的帮助!

PS. 如果您知道为什么我的 \specialcells{} 在排版输出中出现缩进,我也很想知道...

答案1

我不知道\SweaveOpts是什么所以我只是把它注释掉了。

您的两个问题是由额外的空间引起的:之前的空白行\textbf{Function}

\newcommand{\specialcell}[2][t]{
\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}

最好将完整的宏定义放在一行中(如下所示),或者%在编写宏时将 放在内部行的末尾。根据宏的不同,如果不这样做,可能不会引入额外的空间,但正如您的示例所示,通常会引入额外的空间。

如果你摆脱这两个问题,你的问题就会消失,你的输出如下所示:

在此处输入图片描述

完整代码如下:

\documentclass{report}

\usepackage{longtable}

% Define an easy way to make line breaks within tables
\newcommand{\specialcell}[2][t]{\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}

%===----- Functions for indexing and typesetting -----=====
\newcommand{\fun}[1]{\texttt{#1}\index{#1}}
\newcommand{\FUN}[1]{\texttt{#1}\index{#1|textbf}}
\newcommand{\code}[1]{\texttt{#1}}

\begin{document}
%\SweaveOpts{concordance=TRUE}

\begin{longtable}{p{0.12\textwidth} | p{0.5\textwidth} | p{0.38\textwidth}}
  \label{tab:basicfunctions}%
  \textbf{Function} & \textbf{What it does} & \textbf{Example use} \\ \hline
  \endhead
  %
  \fun{:} & A handy little function that generates a sequence of numbers, counting by 1, backwards or forwards. & \specialcell{\code{0:18} \\ \code{48:-8}} \\
  \fun{?} & Finds help files for a specific function, package or dataset. & \code{?max}\\
  \fun{??} & Finds help files that match a given topic & \code{??correlation}\\
  \fun{c} & Combines input to form a vector. Input must be of the same data type (e.g., only letters, only numbers, etc.) & \specialcell{\code{c(1,2,5,9)}\\ \code{c("imagine","that")}} \\
  \fun{cor} & Returns the correlation between two vectors, or between the columns of a matrix. & \code{cor(c(1,3,5,7),c(4,2,8,6))}\\
  \fun{cumsum} & Calculates the cumulative sum of its input. For vectors, it returns a vector. For matrices, it adds down a column first, then continues with the next column. The result is returned as a vector. & \code{cumsum(c(5,10,2,9))}\\
  \fun{diff} & Calculates the sequential difference between a series of numbers. & \code{diff(c(2,4,5,8))}\\
  \fun{dir} & Lists the files in a directory or folder. Default is the current directory, but other locations can be provided. The same as \fun{list.files}. & \specialcell{\code{dir()}\\\code{dir("C:/")} (Windows) \\ \code{dir("/Users/")} (Mac)} \\
  \fun{example} & Runs the example code at the bottom of the help page for a given function. & \code{example(cumsum)} \\
  \fun{head} & Shows the first six elements of a vector or the first six rows of a matrix. & \code{head(letters)} \\
  \FUN{intersect} & Returns the matching elements of two vectors. & \code{intersect(c(2,4),c(1,2))} \\
  \fun{length} & Returns the length of a vector. For a matrix, it returns the total number of elements. & \code{length(LETTERS)} \\
  \fun{list.files} & The same as \code{dir}; provides a list of files in a directory. & \specialcell{\code{list.files()}\\ \code{list.files("C:/")} (Windows) \\ \code{list.files("/Users/")} (Mac)} \\
  \fun{ls} & Gives the names of all the objects currently in working memory. & \code{ls()}\\
  \fun{max} & Returns the largest value in its input. & \code{max(1:100)} \\
  \fun{mean} & Returns the arithmetic average of its input. & \code{mean(c(1,2,3,10))} \\
  \fun{median} & Returns the middle value of its input. & \code{median(c(1,2,3,10)} \\
  \fun{min} & Returns the smallest value in its input. & \code{min(1:100)}\\
  \fun{nchar} & Gives the number of characters in each element of a vector containing strings of text. & \code{nchar(c("imagine","that"))}\\
  \fun{order} & Returns the order of elements in a vector. Note that this does not sort the vector. Instead, it tells you how the elements of the vector would need to be rearranged to sort them. & \code{order(c(9,5,8,2))} \\
  \fun{rep} & Repeats its input a given number of times. Can be used on numbers, characters, and other kinds of data. & \specialcell{\code{rep(1,5)} \\ \code{rep(1:3,5)} \\ \code{rep("okay",10)}} \\
  \fun{rev} & Reverses the contents of its input, and returns them as a vector. & \code{rev(c("is","it","cool"))} \\
\end{longtable}

\end{document}

答案2

您的表格产生了一些边距溢出。另外,我认为您不应该重新发明轮子:已经有一个包(makecell)允许在单元格中换行。所以我建议使用ltablex包,它将的功能带到了longtabletabularx使用tabularx环境将避免溢出到边距。此外,我删除了垂直线,转而使用规则booktabs,这些规则周围有一些垂直填充,并加载了cellspace包,它定义了单元格上方和下方的最小垂直间距,以便不同的行明显不同。最后,我简化了您的代码:您实际上不需要\code在最后一列中使用:

\documentclass{report}
\usepackage{geometry} \usepackage{booktabs, ltablex}
\usepackage{makecell} % Define an easy way to make line breaks within tables
\renewcommand\cellalign{lt}
\renewcommand\theadfont{\rmfamily\normalsize\bfseries}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
%===----- Functions for indexing and typesetting -----=====
\newcommand{\fun}[1]{\texttt{#1}\index{#1}}
\newcommand{\FUN}[1]{\texttt{#1}\index{#1|textbf}}
\newcommand{\code}[1]{\texttt{#1}}

\begin{document}
%\SweaveOpts{concordance=TRUE}

\keepXColumns
\begin{tabularx}{\linewidth}{SlX >{\ttfamily}Sl}
\label{tab:basicfunctions}
%\toprule
\thead{Function} & \thead{What it does} & \thead{Example use} \\[-1ex] \toprule
\endhead
\midrule
\endfoot
\bottomrule
\endlastfoot
%
\fun{:} & A handy little function that generates a sequence of numbers, counting by 1, backwards or forwards. & \makecell{0:18 \\ 48:-8} \\
\fun{?} & Finds help files for a specific function, package or dataset. & ?max \\
\fun{??} & Finds help files that match a given topic & ??correlation \\
\fun{c} & Combines input to form a vector. Input must be of the same data type (e.g., only letters, only numbers, etc.) & \makecell{c(1,2,5,9)\\ c("imagine","that")} \\
\fun{cor} & Returns the correlation between two vectors, or between the columns of a matrix. & cor(c(1,3,5,7),c(4,2,8,6))\\
\fun{cumsum} & Calculates the cumulative sum of its input. For vectors, it returns a vector. For matrices, it adds down a column first, then continues with the next column. The result is returned as a vector. & cumsum(c(5,10,2,9)) \\
\fun{diff} & Calculates the sequential difference between a series of numbers. & diff(c(2,4,5,8)) \\
\fun{dir} & Lists the files in a directory or folder. Default is the current directory, but other locations can be provided. The same as \fun{list.files}. & \makecell{dir()\\dir("C:/") (Windows) \\ dir("/Users/") (Mac)} \\
\fun{example} & Runs the example code at the bottom of the help page for a given function. & example(cumsum) \\
\fun{head} & Shows the first six elements of a vector or the first six rows of a matrix. & head(letters) \\
\FUN{intersect} & Returns the matching elements of two vectors. & intersect(c(2,4),c(1,2)) \\
\fun{length} & Returns the length of a vector. For a matrix, it returns the total number of elements. & length(LETTERS) \\
\fun{list.files} & The same as \code{dir}; provides a list of files in a directory. & \makecell{list.files()\\ list.files("C:/") (Windows) \\ list.files("/Users/") (Mac)} \\
\fun{ls} & Gives the names of all the objects currently in working memory. & ls()\\
\fun{max} & Returns the largest value in its input. & max(1:100) \\
\fun{mean} & Returns the arithmetic average of its input. & mean(c(1,2,3,10)) \\
\fun{median} & Returns the middle value of its input. & median(c(1,2,3,10) \\
\fun{min} & Returns the smallest value in its input. & min(1:100) \\
\fun{nchar} & Gives the number of characters in each element of a vector containing strings of text. & nchar(c("imagine","that")) \\
\fun{order} & Returns the order of elements in a vector. Note that this does not sort the vector. Instead, it tells you how the elements of the vector would need to be rearranged to sort them. & order(c(9,5,8,2)) \\
\fun{rep} & Repeats its input a given number of times. Can be used on numbers, characters, and other kinds of data. & \makecell{rep(1,5) \\ rep(1:3,5) \\ rep("okay",10)} \\
\fun{rev} & Reverses the contents of its input, and returns them as a vector. & rev(c("is","it","cool")) \\
\end{tabularx}

\end{document} 

在此处输入图片描述 在此处输入图片描述

相关内容