我需要画几个一个表格中不同宽度的虚线,但我不知道该怎么做。
我知道如何使用包绘制单条虚线arydshln
,但如何设置这些虚线的宽度?
\setheight\arrayrulewidth{1pt}
仅适用于整个表格,而不适用于特定的单条线。
这是代码。
\documentclass{article}
\usepackage{arydshln}
\begin{document}
% \setheight\arrayrulewidth{1pt} % works for the whole table
\begin{tabular}{cc}
\hdashline % need set to 0.8pt
a & b\\
\hdashline % need set to 0.6pt
\end{tabular}
\end{document}
我的解决方案
我使用包ehhline
来dashrule
得到我想要的东西。
\documentclass{article}
\usepackage{colortbl}
\usepackage{dashrule}
\usepackage{ehhline}
\usepackage{arydshln}
\newcommand\cdl[2]{\leaders\hbox{\textcolor{#1}{\hdashrule{0.4mm}{#2}{0.2mm 0.2mm}}}\hfil}
\newcommand\crs[2]{\leaders\hbox{\textcolor{#1}{\rule{0.1pt}{#2}}}\hfil}
\begin{document}
\begin{tabular}{|c|c|}
\hhline{
!{\cdl{red}{1pt}}
!{\crs{red}{1pt}}
}
test some thing & test\\
\hhline{
!{\cdl{blue}{4pt}}
!{\crs{blue}{4pt}}
}
test some thing & test\\
\hhline{
!{\cdl{blue}{4pt}}
!{\crs{blue}{4pt}}
}
test some thing & test\\
\hhline{
!{\cdl{blue}{4pt}}
!{\crs{blue}{4pt}}
}
\end{tabular}
\end{document}
仍未解决
但还有一个问题:如何绘制垂直虚线不同颜色和宽度
答案1
{NiceTabular}
在 (2020-05-08 ≥ 4.0)的环境下,你可以使用数组下nicematrix
创建的 PGF/Tikz 节点用 Tikz 绘制任何你想要的规则。nicematrix
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\tikzset
{
dashed 1/.style =
{
red ,
dash pattern = on 2pt off 3pt ,
line width = 1 pt
} ,
dashed 2/.style =
{
blue ,
dash pattern = on 1pt off 1pt ,
line width = 2pt
}
}
\begin{NiceTabular}{|ccc|}
\hline
text text & text text & text text \\
text text & text text & text text \\
text text & text text & text text \\
text text & text text & text text \\
text text & text text & text text \\
\hline
\CodeAfter
\begin{tikzpicture}
\draw [dashed 1] (1-|2) -- (6-|2) (2-|1) -- (2-|4) ;
\draw [dashed 2] (1-|3) -- (6-|3) (4-|1) -- (4-|4) ;
\end{tikzpicture}
\end{NiceTabular}
\end{document}
使用较新版本nicematrix
(2022-07-28 的 6.12 版),还可以定义字母(用于垂直规则)和命令(用于水平规则)以使用我们想要的任何 Tikz 样式绘制规则。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\NiceMatrixOptions
{
custom-line =
{
letter = I ,
command = DashedLine ,
tikz =
{
red ,
dash pattern = on 2pt off 3pt ,
line width = 1 pt
} ,
total-width = 1 pt % space reserved for the rule
} ,
custom-line =
{
letter = J ,
command = DashedLineBis ,
tikz =
{
blue ,
dash pattern = on 1pt off 1pt ,
line width = 2pt
} ,
total-width = 2 pt
}
}
\begin{NiceTabular}{|cIcJc|}
\hline
text text & text text & text text \\
\DashedLine
text text & text text & text text \\
text text & text text & text text \\
\DashedLineBis
text text & text text & text text \\
text text & text text & text text \\
\hline
\end{NiceTabular}
\end{document}
答案2
我对领导者没有太多经验,所以唯一能让它发挥作用的方法是先测量表格的宽度。为此,我创建了环境mytabular
。
我没有实现双虚线或虚线\cline
。
\documentclass{article}
\usepackage{environ}
\newsavebox{\mybox}
\newcommand{\dashline}[3]% #1 = dash, #2 = spacing, #3 = height
{\noalign{\hbox to \wd\mybox{\leaders\hbox to #2{\hss\rule{#1}{#3}\hss}\hfill}}}
\newcommand{\gobbleline}[3]{\noalign{\hrule}}
\NewEnviron{mytabular}[1]% #1 = tabular parameters
{\savebox{\mybox}{\let\dashline=\gobbleline% disable \dashline
\begin{tabular}{#1}
\BODY
\end{tabular}}% measure width
\begin{tabular}{#1}
\BODY
\end{tabular}}
\begin{document}
\begin{mytabular}{cc}
\dashline{0.8pt}{4pt}{1pt}
a & b \\
\dashline{0.4pt}{4pt}{0.5pt}
\end{mytabular}
\end{document}
或者,可以重新定义表格环境,但这将花费旧环境的两倍时间。
\documentclass{article}
\usepackage{environ}
\newsavebox{\mybox}
\newcommand{\dashline}[3]% #1 = dash, #2 = spacing, #3 = height
{\noalign{\hbox to \wd\mybox{\leaders\hbox to #2{\hss\rule{#1}{#3}\hss}\hfill}}}
\newcommand{\gobbleline}[3]{\noalign{\hrule}}
\let\oldtabular=\tabular
\let\oldendtabular=\endtabular
\RenewEnviron{tabular}[1]% #1 = normal tabular parameters
{\savebox{\mybox}{\let\dashline=\gobbleline% disable \dashline
\oldtabular{#1}
\BODY
\oldendtabular}% measure width
\oldtabular{#1}
\BODY
\oldendtabular}
\begin{document}
\begin{tabular}{cc}
\dashline{0.8pt}{4pt}{1pt}
a & b \\
\dashline{0.4pt}{4pt}{0.5pt}
\end{tabular}
\end{document}