下面是一个例子,对我来说似乎比较复杂。我只能制作一个带暗线的表格,不知道如何将暗线变成空心的。
有没有办法在 LaTeX 中嵌入这么漂亮的表格?
答案1
这就是为什么每个人都说“去掉垂直线”。到处都是双线,这只会让灾难进一步加剧。
\documentclass{article}
\usepackage{hhline}
\usepackage{caption}
\usepackage{booktabs}
\begin{document}
\begin{table}[htb]
\centering
\sffamily
\captionsetup{font={bf,sf},skip=0.5ex}
\caption*{Access Levels}
\begin{tabular}{||*{5}{l||}}
\hhline{|t:=:t:=:t:=:t:=:t:=:t|}
\bfseries Modifier & \bfseries Class & \bfseries Package & \bfseries Subclass & \bfseries World \\
\hhline{|:=::=::=::=::=:|}
\texttt{public} & Y & Y & Y & Y \\
\hhline{|:=::=::=::=::=:|}
\texttt{protected} & Y & Y & Y & N \\
\hhline{|:=::=::=::=::=:|}
\itshape no modifier & Y & Y & N & N \\
\hhline{|:=::=::=::=::=:|}
\texttt{private} & Y & N & N & N \\
\hhline{|b:=:b:=:b:=:b:=:b:=:b|}
\end{tabular}
\end{table}
\begin{table}[htb]
\centering
\sffamily
\captionsetup{font={bf,sf},skip=0.5ex}
\caption*{Access Levels}
\begin{tabular}{l*{4}{c}}
\toprule
\bfseries Modifier & \bfseries Class & \bfseries Package & \bfseries Subclass & \bfseries World \\
\midrule
\texttt{public} & Y & Y & Y & Y \\
\texttt{protected} & Y & Y & Y & N \\
\itshape no modifier & Y & Y & N & N \\
\texttt{private} & Y & N & N & N \\ \bottomrule
\end{tabular}
\end{table}
\end{document}
自己决定哪一个看起来不错。我喜欢第二个。
正如 touhami 所指出的,添加后\setlength\tabcolsep{1pt}
产生的图像看起来更类似于问题中的图像,但在我看来它很拥挤。
您可能希望使用颜色来使其Y
更N
显眼。
\documentclass{article}
\usepackage{hhline}
\usepackage{caption}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand*{\myred}{\cellcolor{red!50}}
\newcommand*{\mygreen}{\cellcolor{green!50}}
\newcommand*{\mycolor}{\cellcolor{olive!40}}
\begin{document}
\begin{table}[htb]
\arrayrulecolor{white}
\doublerulesepcolor{gray!20}
\centering
\sffamily
\captionsetup{font={bf,sf},skip=0.5ex}
\caption*{Access Levels}
\begin{tabular}{||*{5}{c||}}
\hhline{|t:=:t:=:t:=:t:=:t:=:t|}
\rowcolor{blue!30}
\bfseries Modifier & \bfseries Class & \bfseries Package & \bfseries Subclass & \bfseries World \\ \hhline{|:=::=::=::=::=:|}
\mycolor\texttt{public} & \mygreen Y & \mygreen Y & \mygreen Y & \mygreen Y \\
\hhline{|:=::=::=::=::=:|}
\mycolor\texttt{protected} & \mygreen Y & \mygreen Y & \mygreen Y & \myred N \\
\hhline{|:=::=::=::=::=:|}
\mycolor\itshape no modifier & \mygreen Y & \mygreen Y & \myred N & \myred N \\
\hhline{|:=::=::=::=::=:|}
\mycolor\texttt{private} & \mygreen Y & \myred N & \myred N & \myred N \\
\hhline{|b:=:b:=:b:=:b:=:b:=:b|}
\end{tabular}
\end{table}
\end{document}
答案2
我不同意你的观点,认为问题中给出的表格很好。我不喜欢 OP 仅使用 tex.sx.com 作为“代码生成器”(即放图片并等待代码)的问题。但如果这里介绍了 LaTeX 解决方案,那么我可以展示纯 TeX 解决方案(只是为了完整性)。
双线表可以通过以下方式简单创建:
\input opmac
\input chelvet % Helvetica plus Courier
\def\tabstrut{\lower4pt\vbox to15pt{}} % more vertical space
\hfil {\bf Access Levels}
\smallskip
\hfil\frame{\table{|l||l||l||l||l|}{\crli
\bf Modifier & \bf Class & \bf Package & \bf Subclass & \bf World \crlli
\tt public & Y & Y & Y & Y \crlli
\tt protected & Y & Y & Y & N \crlli
\it no modifier & Y & Y & N & N \crlli
\tt private & Y & N & N & N \crli
}}
\bye
结果:
如果需要彩色版本,那么我们需要从 OPmac-tricks 页面复制几行,因为 OPmac 默认不支持彩色单元格。
\input opmac
\input chelvet % Helvetica plus Courier
\def\tabstrut{\lower4pt\vbox to15pt{}} % more vertical space
%%% The code from http://petr.olsak.net/opmac-tricks-e.html#cellcolor
\def\tabdeclareC{\futurelet\next\setcellcolor##\end\hfil\hfil}
\def\setcellcolor{\ifx\next\global \expandafter\setcellcolorC\else \expandafter\setcellcolorD\fi}
\def\setcellcolorC#1\fi#2\end#3#4{%
\setbox0=\hbox{{\tabiteml\localcolor#2\unskip\tabitemr}}%
{\localcolor#1\fi\tabstrut\leaders\vrule\hskip\wd0 \ifx#3\hfil plus1fil\fi}%
\kern-\wd0 \box0
\ifx#4\hfil {\kern-.2pt \localcolor#1\fi\leaders\vrule\hskip.2pt plus1fil}\fi
}
\def\setcellcolorD{\ifx\cellcolor\undefined \let\next=\setcellcolorN
\else \def\next{\expandafter\expandafter\expandafter\setcellcolorC\cellcolor}%
\fi \next
}
\def\setcellcolorN#1\end#2#3{#2\tabiteml{\localcolor#1\unskip}\tabitemr#3}
%%% The table:
\def\LightGreen{\setcmykcolor{.5 0 .5 0}}
\def\LightRed{\setcmykcolor{0 .5 .5 0}}
\def\LightBlue{\setcmykcolor{.3 .1 0 0}}
\hfil {\bf Access Levels}
\smallskip
{\tabskip=2pt \def\cskip{\noalign{\kern2pt}}
\let\cellcolor=\LightBlue \def\Y{\LightGreen Y} \def\N{\LightRed N}
\hfil\table{CCCCC}{
\bf Modifier & \bf Class & \bf Package & \bf Subclass & \bf World \cr \cskip
\tt public & \Y & \Y & \Y & \Y \cr \cskip
\tt protected & \Y & \Y & \Y & \N \cr \cskip
\it no modifier & \Y & \Y & \N & \N \cr \cskip
\tt private & \Y & \N & \N & \N
}}
\bye
结果: