
xcolor
옵션 이 있는 패키지를 사용하여 행 색상이 교대로 나타나는 테이블을 만들려고 합니다 [table]
. 다음 예에서는 행 색상 지정( 주석 해제 \rowcolors..
)이 테이블 내용을 숨기거나 가릴 수 있음을 보여줍니다. 특히 @-표현식에 문제가 있는 것 같습니다.
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
%\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
- 이를 어떻게 방지할 수 있나요?
- 두 번째 질문은 전체 라인을 색상으로 덮는 쉬운 방법이 있는지 여부입니다. 회색 선에서 흰색 패딩을 볼 수 있습니다.
답변1
\tabcolsep
첫 번째 질문의 경우 다음과 같은 두 열에 대해 별도로 제거할 수 있습니다 .
r<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
두 번째로 \bottomrulec
좋아요를 정의할 수 있습니다.
\newcommand{\bottomrulec}{%
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
대신에 그것을 사용하십시오 \bottomrule
. 필요한 경우를 \toprule
대비해 컬러 버전이 있습니다 .\midrule
\newcommand{\toprulec}{%
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\aboverulesep}{0pt}
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}
}
\newcommand{\midrulec}{%
\arrayrulecolor{gray!15}\specialrule{\aboverulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{\belowrulesep}
}
수정된 코드:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\newcommand{\bottomrulec}{% Coloured \toprule
\arrayrulecolor{gray!15}\specialrule{\belowrulesep}{0pt}{0pt}
\arrayrulecolor{black}\specialrule{\heavyrulewidth}{0pt}{0pt}
\arrayrulecolor{black}
}
\begin{document}
\begin{table}
\footnotesize
\centering
\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr<{\hspace{-\tabcolsep}}>{\hspace{-\tabcolsep}\,}c
<{\hspace{-\tabcolsep}\,}>{\hspace{-\tabcolsep}}lcc}
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrulec
\end{tabular}
\end{table}
\end{document}
답변2
공백은 booktabs
테이블 규칙 위와 아래에 수직 패딩을 추가하여 발생합니다. 해결 방법은 이 패딩을 $0$pt로 설정하고 이를 패키지의 수직 간격으로 바꾸는 것으로 구성됩니다 cellspace
.최소한의 이러한 수직 간격은 다음과 호환됩니다 \rowcolors
(열 지정자 앞에 문자가 와야 함 S
).
에 대한 문제는 @{}
두 번째 열의 셀을 떠나 세 번째 열에 입력할 때 음수 수평 간격을 추가하는 것으로 대체 >{}
합니다 <{}
.
\documentclass[preview]{article}
\usepackage{mathtools}
\usepackage{tabularx, booktabs, caption, array}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{cellspace}
\setlength\cellspacetoplimit{6pt}
\setlength\cellspacebottomlimit{6pt}
\begin{document}
\begin{table}
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\rowcolors{2}{gray!15}{white}% <{\hskip-\arraycolsep}>{\hskip-\arraycolsep{}}
$ \begin{array}{Slr <{{}\hskip-\arraycolsep}>{\hskip-\arraycolsep\mkern-.5mu}lcc}
&& & \text{Col 1} & \text{Col 2} \\
\toprule
\text{Row 1} & (100 \times{} & 100) & 1 & 2 \\
\text{Row 2} & (100 \times{} &1000) & 3 & 4 \\
\bottomrule
\end{array} $
\end{table}
\end{document}
답변3
{NiceTabular}
의 환경은 PGF/Tikz를 사용하여 드로잉하는 nicematrix
것과 유사한 도구를 제공합니다 .colortbl
해당 환경을 사용하면 원하는 것을 직접 얻을 수 있습니다(그러나 nicematrix
PGF/Tikz 노드를 사용하므로 여러 컴파일이 필요합니다).
\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}
\begin{document}
\begin{table}
\footnotesize
\centering
\begin{NiceTabular}{lr@{\hspace{.1em}}c@{\hspace{.1em}}lcc}
\CodeBefore
\rowcolors{2}{gray!15}{}
\Body
&&&& Col 1 & Col 2 \\ \toprule
Row 1 & $(100$ & $\times$ & $100)$ & $1$ & $2$ \\
Row 2 & $(100$ & $\times$ & $1000)$& $3$ & $4$ \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}