这个问题可能很愚蠢,在网上查过之后就可以找到答案,但是我还是来这里寻求帮助。
我想使用 LaTeX 创建一个表格。表格如下所示:
我设法制作了一个表格,但它不是我所设想的那样。它看起来像这样:
我使用的代码如下:
\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage{array}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tabularx,ragged2e,booktabs,caption}
\renewcommand\tabularxcolumn[1]{C{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\setlength\parindent{0pt}
\begin{document}
\sffamily\small
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\noindent
\centering
\begin{tabularx}{\linewidth}{|p{2.0cm}|p{2.0cm}| >{\compress\RaggedRight}X|*{1}{p{21mm}|}}
\hline
Word & Trials & Processing time (in seconds)\\
\hline
%\multirow{2}{=}
{total} & 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
\hline
\end{tabularx}
\label{tab:tab1}
\caption{Tabulated app's responses to different usernames and passwords.}
\end{document}
我的问题是:
我们如何使用 LaTeX 制作一个看起来像上面第一张图片的表格,并且看起来更有吸引力?
答案1
您实际上不需要 tabularx,我已将其删除。我使用 tabular 中的 tabular 和 columntypem{}
来创建表格。
代码如下:
\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage{array}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ragged2e,booktabs,caption}
\setlength\parindent{0pt}
\begin{document}
\sffamily\small
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\begin{table}
\centering
\begin{tabular}{|>{\centering\arraybackslash}m{2cm}| >{\centering\arraybackslash}m{3cm} |>{\centering\arraybackslash}m{4cm}|}
\hline
Word & Trials & \begin{tabular}{c}Processing time \\(in seconds)\end{tabular}\\
\hline
\multirow{10}{*}
{``total''} & 1 & Here can be a sentence that will be centered and break automatically \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\cline{2-3}
& 1 & 2 secs \\
\hline
\end{tabular}
\label{tab:tab1}
\caption{Tabulated app's responses to different usernames and passwords.}
\end{table}
\end{document}
输出:
关于更美观(第一条规则是不要使用垂直线,如果可能的话,在不需要时不要使用水平线。)一个例子:
\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,booktabs}
\usepackage{amssymb}
\usepackage[table]{xcolor}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ragged2e,booktabs,caption}
\setlength\parindent{0pt}
\begin{document}
\sffamily\small
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\begin{table}
\centering
\begin{tabular}{>{\centering\arraybackslash}m{2cm} >{\centering\arraybackslash}m{3cm} >{\centering\arraybackslash}m{4cm}}\arrayrulecolor{red}\toprule
\rowcolor{gray!60}
Word & Trials & \begin{tabular}{c}
Processing time \\(in seconds)\end{tabular}\\\rowcolor{gray!20}\midrule
\cellcolor{gray!40} & 1 & 2 secs \\\rowcolor{gray!40}
&2 & 2 secs \\\rowcolor{gray!20}
\cellcolor{gray!40}&3 & 2 secs \\\rowcolor{gray!40}
&4 & 2 secs \\\rowcolor{gray!20}
\cellcolor{gray!40}&5 & 2 secs \\\rowcolor{gray!40}
\multirow{-6}{*}{\cellcolor{gray!40}``total''}&6 & 2 secs\\\bottomrule
\end{tabular}
\label{tab:tab1}
\caption{Tabulated app's responses to different usernames and passwords.}
\end{table}
\end{document}
得出:
答案2
只是为了好玩一个选项使用禁忌,通过一些选项来实现您所寻找的东西,它并不是很流行,因为自 2011 年以来它就缺乏支持,但我更喜欢使用它,因为它的编码干净,当然因为它是 tabular、tabularx 和 array 等包的扩展;并且作为衍生产品仅限于它自己的命令,但不会拒绝来自父库的命令。
在这个例子中,表格的线条宽度是变化的,这在使用多行时给禁忌带来了问题,但通过定义一个用于表格线条宽度的变量来解决,该变量将在每一行中使用,以纠正间隙\\[-table_line_width]
,这是用户发现的东西。
结果:
梅威瑟:
% arara: pdflatex: {synctex: yes, action: nonstopmode}
\documentclass[a4paper,twocolumn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{booktabs}
\usepackage{amssymb}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{ragged2e,booktabs,caption}
\usepackage[scaled]{helvet}% for sans serif family
\renewcommand\familydefault{\sfdefault}
%Packages for beautifull tables.
\usepackage{tabu}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{array}
\usepackage[longtable]{multirow}
\usepackage[table]{xcolor}
\usepackage{lipsum} % Dummy text
\begin{document}
\lipsum[1]
\begin{table}[!h]
\def\TableLineSize{1.5pt}
\small
\tabulinesep = 2.5pt
\tabulinestyle{\TableLineSize cyan!50!lime}
\begin{tabu} to \linewidth {|X[1,mc]|X[1.5,mc]|X[1.5,mc]|} \tabucline -
\rowcolor{cyan!50!lime!20!white}
\rowfont[c]{\bf\color{cyan!50!lime!50!black}}
Word & Trials & Processing time (in seconds)\\ \tabucline -
\everyrow{\tabucline {2-3}}
\multirow{10}{*}{Total}
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
& 1 & 2 secs \\[-\TableLineSize]
\everyrow{}
& 1 & 2 secs \\ \tabucline -
\end{tabu}
\caption{Tabulated app's responses to different usernames and passwords.}
\label{tab:tab1}
\end{table}
\lipsum[4-9]
\end{document}