선을 망치지 않고 라텍스 테이블의 셀을 올바르게 색칠하는 방법은 무엇입니까?

선을 망치지 않고 라텍스 테이블의 셀을 올바르게 색칠하는 방법은 무엇입니까?

다음과 같은 문제가 있습니다. 값별로 셀의 색상을 지정하고 싶습니다. nSHD의 값은 (0,1)이고 값이 작을수록 더 좋습니다. 따라서 0에 가까운 값은 녹색이고 큰 값은 노란색이 됩니다. 반면에 TPR에는 (0,1)의 값도 있지만 값이 클수록 더 좋으므로 반대를 원합니다(녹색은 1에 가까운 값, 노란색은 작은 값). 이제 문제는 선을 색상과 결합하지 못한다는 것입니다. 나는 이렇게 시작했다:

\documentclass{article}


\usepackage{array}
\newcolumntype{?}{!{\vrule width 1.2pt}}
\usepackage{collcell}

\usepackage[dvipsnames,table]{xcolor}


\usepackage{multirow}
\usepackage{multicol}


\usepackage{pgf} % for calculating the values for gradient
%======================================
% Color set related!
\definecolor{high}{HTML}{00994d}  % the color for the highest number in your data set
\definecolor{low}{HTML}{fff51b}  % the color for the lowest number in your data set
\newcommand*{\opacity}{70}% here you can change the opacity of the background color!
%======================================
% Data set related!
\newcommand*{\minval}{0}% define the minimum value on your data set
\newcommand*{\maxval}{0.25}% define the maximum value in your data set!
\newcommand*{\minvall}{0.75}% define the minimum value on your data set
\newcommand*{\maxvall}{1.0}% define the maximum value in your data set!
%======================================
% gradient function!
\newcommand{\grhigh}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxvall pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minvall pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxvall-\minvall))-(\minvall*(100/(\maxvall-\minvall)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{high!\tempa!low!\opacity} #1
    }}
}
\newcommand{\grlow}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxval pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minval pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxval-\minval))-(\minval*(100/(\maxval-\minval)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{low!\tempa!high!\opacity} #1
    }}
}


\begin{document}





\renewcommand{\arraystretch}{1.6}

\begin{table}[!htbp]
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|c|c|c|c|c|c?c|c|c|c?c|c|c|c|}
\hline
\multicolumn{2}{|c|}{$r$} &
\multicolumn{4}{c?}{$10\%$} &
\multicolumn{4}{c?}{$30\%$} &
\multicolumn{4}{c|}{$50\%$} \\
\hline
\multicolumn{2}{|c|}{$n$} & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500  \\
\hline
\multirow{2}{*}{$d=10$} & nSHD & \grlow{0.19} & \grlow{0.11} & \grlow{0.04} & \grlow{0.03} & \grlow{0.13} & \grlow{0.1} & \grlow{0.03} & \grlow{0.03} & \grlow{0.18} & \grlow{0.15} & \grlow{0.09} & \grlow{0.08} \\ \cline{2-14}
 & TPR & \grhigh{0.88} & \grhigh{0.94} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.91} & \grhigh{0.93} & \grhigh{0.95} & \grhigh{0.94} \\
\hline
\multirow{2}{*}{$d=20$} & nSHD   & \grlow{0.11} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.13} & \grlow{0.09} & \grlow{0.06} & \grlow{0.04} & \grlow{0.18} & \grlow{0.14} & \grlow{0.1} & \grlow{0.08} \\ \cline{2-14}
 & TPR & \grhigh{0.94} & \grhigh{0.98} & \grhigh{1.0} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.86} & \grhigh{0.92} & \grhigh{0.94} & \grhigh{0.96} \\
\hline
\multirow{2}{*}{$d=30$} & nSHD  & \grlow{0.1} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.12} & \grlow{0.08} & \grlow{0.05} & \grlow{0.03} & \grlow{0.15} & \grlow{0.12} & \grlow{0.09} & \grlow{0.07} \\ \cline{2-14}
& TPR & \grhigh{0.96} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.89} & \grhigh{0.9} & \grhigh{0.95} & \grhigh{0.96} \\
\hline
\end{tabular}
}
\vspace*{2mm}
\caption{Some Caption.} 
\end{table}

\end{document}

이것은 색상에는 잘 작동했지만 선을 제대로 그리지 못했습니다. 예를 들어 Adobe로 열면 다음과 같습니다. 누락된 줄

수직선과 수평선이 제대로 그려지지 않은 것을 볼 수 있습니다. 그런 다음 StackExchange에서 여기를 검색하여 다음과 유사한 질문을 찾았습니다.

Adobe에서 문제를 표시하는 표 형식 및 줄

두 가지 해결 방법이 제안되었습니다. 첫째, 작동하지 않는 nicematrix 패키지를 사용한 다음 테이블을 두 번 그립니다. 한 번은 색상이 있고 한 번은 색상이 없습니다. 이 답변을 바탕으로 다음을 시도했습니다.

\documentclass{article}


\usepackage{array}
\newcolumntype{?}{!{\vrule width 1.2pt}}
\usepackage{collcell}

\usepackage[dvipsnames,table]{xcolor}


\usepackage{multirow}
\usepackage{multicol}


\usepackage{pgf} % for calculating the values for gradient
%======================================
% Color set related!
\definecolor{high}{HTML}{00994d}  % the color for the highest number in your data set
\definecolor{low}{HTML}{fff51b}  % the color for the lowest number in your data set
\newcommand*{\opacity}{70}% here you can change the opacity of the background color!
%======================================
% Data set related!
\newcommand*{\minval}{0}% define the minimum value on your data set
\newcommand*{\maxval}{0.25}% define the maximum value in your data set!
\newcommand*{\minvall}{0.75}% define the minimum value on your data set
\newcommand*{\maxvall}{1.0}% define the maximum value in your data set!
%======================================
% gradient function!
\newcommand{\grhigh}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxvall pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minvall pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxvall-\minvall))-(\minvall*(100/(\maxvall-\minvall)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{high!\tempa!low!\opacity} #1
    }}
}
\newcommand{\grlow}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxval pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minval pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxval-\minval))-(\minval*(100/(\maxval-\minval)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{low!\tempa!high!\opacity} #1
    }}
}


\begin{document}





\renewcommand{\arraystretch}{1.6}

\begin{table}[!htbp]
\centering
\resizebox{\columnwidth}{!}{%
\def\tmp{
\begin{tabular}{|c|c|c|c|c|c?c|c|c|c?c|c|c|c|}
\hline
\multicolumn{2}{|c|}{$r$} &
\multicolumn{4}{c?}{$10\%$} &
\multicolumn{4}{c?}{$30\%$} &
\multicolumn{4}{c|}{$50\%$} \\
\hline
\multicolumn{2}{|c|}{$n$} & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500  \\
\hline
\multirow{2}{*}{$d=10$} & nSHD & \grlow{0.19} & \grlow{0.11} & \grlow{0.04} & \grlow{0.03} & \grlow{0.13} & \grlow{0.1} & \grlow{0.03} & \grlow{0.03} & \grlow{0.18} & \grlow{0.15} & \grlow{0.09} & \grlow{0.08} \\ \cline{2-14}
 & TPR & \grhigh{0.88} & \grhigh{0.94} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.91} & \grhigh{0.93} & \grhigh{0.95} & \grhigh{0.94} \\
\hline
\multirow{2}{*}{$d=20$} & nSHD   & \grlow{0.11} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.13} & \grlow{0.09} & \grlow{0.06} & \grlow{0.04} & \grlow{0.18} & \grlow{0.14} & \grlow{0.1} & \grlow{0.08} \\ \cline{2-14}
 & TPR & \grhigh{0.94} & \grhigh{0.98} & \grhigh{1.0} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.86} & \grhigh{0.92} & \grhigh{0.94} & \grhigh{0.96} \\
\hline
\multirow{2}{*}{$d=30$} & nSHD  & \grlow{0.1} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.12} & \grlow{0.08} & \grlow{0.05} & \grlow{0.03} & \grlow{0.15} & \grlow{0.12} & \grlow{0.09} & \grlow{0.07} \\ \cline{2-14}
& TPR & \grhigh{0.96} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.89} & \grhigh{0.9} & \grhigh{0.95} & \grhigh{0.96} \\
\hline
\end{tabular}
}
\leavevmode
\rlap{\tmp}%
\begingroup
  \renewcommand*{\cellcolor}[1]{}%
  \tmp
\endgroup
}
\vspace*{2mm}
\caption{Some Caption.} 
\end{table}





\end{document}

에버리싱 볼드

표를 두 번 그리면 숫자와 선이 매우 두꺼워지는 것을 알 수 있습니다. 누구든지 이 문제에 대한 해결책을 갖고 있습니까?

답변1

tabularray이것은 선이 셀 색상으로 가려지는 문제가 없는 패키지를 사용하는 접근 방식입니다 .

\documentclass{article}
\usepackage[margin=2cm]{geometry}

\usepackage{tabularray}
\UseTblrLibrary{functional}

\usepackage{pgf} % for calculating the values for gradient
%======================================
% Color set related!
\definecolor{high}{HTML}{00994d}    % the color for the highest number in your data set
\definecolor{low}{HTML}{fff51b}     % the color for the lowest number in your data set
\pgfmathtruncatemacro{\opacity}{70} % here you can change the opacity of the background color!
%======================================
% Data set related!
\pgfmathsetmacro{\minval}{0}% define the minimum value on your data set
\pgfmathsetmacro{\maxval}{0.25}% define the maximum value in your data set!
\pgfmathsetmacro{\minvall}{0.75}% define the minimum value on your data set
\pgfmathsetmacro{\maxvall}{1.0}% define the maximum value in your data set!
%======================================
% gradient function!
\IgnoreSpacesOn
    \prgNewFunction \grhighlowColor {} {
        \intStepOneInline {3} {\arabic{rowcount}} {
            \intSet \lTmpaInt { \intMathMod {##1} {2} }
            \intCompareTF {\lTmpaInt} = {1} {
                \intStepOneInline {3} {\arabic{colcount}} {
                    \tlSet \lTmpbTl {\cellGetText {##1} {####1}}
                    \fpCompareTF {\lTmpbTl} > {\maxval} { } {
                        \fpCompareTF {\lTmpbTl} < {\minval} { } {
                            \pgfmathparse{int(round(100*(\lTmpbTl/(\maxval-\minval))-(\minval*(100/(\maxval-\minval)))))}
                            \cellSetStyle {##1} {####1} {bg=low!\pgfmathresult!high!\opacity}
                        }
                    }
                }
            } {
                \intStepOneInline {3} {\arabic{colcount}} {
                    \tlSet \lTmpbTl {\cellGetText {##1} {####1}}
                    \fpCompareTF {\lTmpbTl} > {\maxvall} { } {
                        \fpCompareTF {\lTmpbTl} < {\minvall} { } {
                            \pgfmathparse{int(round(100*(\lTmpbTl/(\maxvall-\minvall))-(\minvall*(100/(\maxvall-\minvall)))))}
                            \cellSetStyle {##1} {####1} {bg=high!\pgfmathresult!low!\opacity}
                        }
                    }
                }
            }
        }
    }
\IgnoreSpacesOff

\begin{document}

\begin{table}\footnotesize
\begin{tblr}{
    colspec = { *{2}{c} *{12}{X[c]} },
    hlines, 
    vlines,
    vline{7,11} = {1.2pt},
    process=\grhighlowColor
}
\SetCell[c=2]{} $r$ & &
\SetCell[c=4]{} $10\%$ & & & &
\SetCell[c=4]{} $30\%$ & & & &
\SetCell[c=4]{} $50\%$ & & & \\
\SetCell[c=2]{} $n$ & & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500  \\
\SetCell[r=2]{} $d=10$ & nSHD & 0.19 & 0.11 & 0.04 & 0.03 & 0.13 & 0.1 & 0.03 & 0.03 & 0.18 & 0.15 & 0.09 & 0.08 \\ 
 & TPR & 0.88 & 0.94 & 0.97 & 0.99 & 0.96 & 0.98 & 0.99 & 1.0 & 0.91 & 0.93 & 0.95 & 0.94 \\
\SetCell[r=2]{} $d=20$ & nSHD & 0.11 & 0.06 & 0.03 & 0.02 & 0.13 & 0.09 & 0.06 & 0.04 & 0.18 & 0.14 & 0.1 & 0.08 \\ 
 & TPR & 0.94 & 0.98 & 1.0 & 1.0 & 0.93 & 0.96 & 0.98 & 0.99 & 0.86 & 0.92 & 0.94 & 0.96 \\
\SetCell[r=2]{} $d=30$ & nSHD & 0.1 & 0.06 & 0.03 & 0.02 & 0.12 & 0.08 & 0.05 & 0.03 & 0.15 & 0.12 & 0.09 & 0.07 \\ 
& TPR & 0.96 & 0.97 & 0.99 & 1.0 & 0.93 & 0.96 & 0.98 & 0.99 & 0.89 & 0.9 & 0.95 & 0.96 \\
\end{tblr}
\vspace*{2mm}
\caption{Some Caption.} 
\end{table}

\end{document}

여기에 이미지 설명을 입력하세요

답변2

{NiceTabular}of를 사용하여 완벽한 출력을 얻으려면 표준 LaTeX의 명령인(그리고 에서 재정의되지 않은 ) nicematrix을 사용하면 안 됩니다 . 그러나 의 명령을 사용하면 사용할 필요가 없습니다 . 블록에 가로 및 세로 규칙이 그려지지 않습니다.\clinenicematrix\cline\Blocknicematrix

\documentclass{article}

\usepackage{collcell}

\usepackage[dvipsnames,table]{xcolor}

\usepackage{nicematrix,tikz}

\usepackage{pgf} % for calculating the values for gradient
%======================================
% Color set related!
\definecolor{high}{HTML}{00994d}  % the color for the highest number in your data set
\definecolor{low}{HTML}{fff51b}  % the color for the lowest number in your data set
\newcommand*{\opacity}{70}% here you can change the opacity of the background color!
%======================================
% Data set related!
\newcommand*{\minval}{0}% define the minimum value on your data set
\newcommand*{\maxval}{0.25}% define the maximum value in your data set!
\newcommand*{\minvall}{0.75}% define the minimum value on your data set
\newcommand*{\maxvall}{1.0}% define the maximum value in your data set!
%======================================
% gradient function!
\newcommand{\grhigh}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxvall pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minvall pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxvall-\minvall))-(\minvall*(100/(\maxvall-\minvall)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{high!\tempa!low!\opacity} #1
    }}
}
\newcommand{\grlow}[1]{
    % The values are calculated linearly between \minval and \maxval
    \ifdimcomp{#1pt}{>}{\maxval pt}{#1}{
        \ifdimcomp{#1pt}{<}{\minval pt}{#1}{
            \pgfmathparse{int(round(100*(#1/(\maxval-\minval))-(\minval*(100/(\maxval-\minval)))))}
            \xdef\tempa{\pgfmathresult}
            \cellcolor{low!\tempa!high!\opacity} #1
    }}
}


\begin{document}

\renewcommand{\arraystretch}{1.6}


\NiceMatrixOptions
  {
    custom-line = 
     {
       letter = ? ,
       total-width = 1.2 pt , 
       tikz = { line width = 1.2 pt } 
     }
  }

\begin{table}[!htbp]
\centering
\resizebox{\columnwidth}{!}{%
\begin{NiceTabular}{|c|c|c|c|c|c?c|c|c|c?c|c|c|c|}[hlines]
\Block{1-2}{$r$} &&
\Block{1-4}{$10\%$} &&&&
\Block{1-4}{$30\%$} &&&&
\Block{1-4}{$50\%$} \\
\Block{1-2}{$n$} && 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500 & 50  & 100  & 250 & 500  \\
\Block{2-1}{$d=10$} & nSHD & \grlow{0.19} & \grlow{0.11} & \grlow{0.04} & \grlow{0.03} & \grlow{0.13} & \grlow{0.1} & \grlow{0.03} & \grlow{0.03} & \grlow{0.18} & \grlow{0.15} & \grlow{0.09} & \grlow{0.08} \\ 
 & TPR & \grhigh{0.88} & \grhigh{0.94} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.91} & \grhigh{0.93} & \grhigh{0.95} & \grhigh{0.94} \\
\Block{2-1}{$d=20$} & nSHD   & \grlow{0.11} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.13} & \grlow{0.09} & \grlow{0.06} & \grlow{0.04} & \grlow{0.18} & \grlow{0.14} & \grlow{0.1} & \grlow{0.08} \\ 
 & TPR & \grhigh{0.94} & \grhigh{0.98} & \grhigh{1.0} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.86} & \grhigh{0.92} & \grhigh{0.94} & \grhigh{0.96} \\
\Block{2-1}{$d=30$} & nSHD  & \grlow{0.1} & \grlow{0.06} & \grlow{0.03} & \grlow{0.02} & \grlow{0.12} & \grlow{0.08} & \grlow{0.05} & \grlow{0.03} & \grlow{0.15} & \grlow{0.12} & \grlow{0.09} & \grlow{0.07} \\ 
& TPR & \grhigh{0.96} & \grhigh{0.97} & \grhigh{0.99} & \grhigh{1.0} & \grhigh{0.93} & \grhigh{0.96} & \grhigh{0.98} & \grhigh{0.99} & \grhigh{0.89} & \grhigh{0.9} & \grhigh{0.95} & \grhigh{0.96} \\
\end{NiceTabular}
}
\vspace*{2mm}
\caption{Some Caption.} 
\end{table}

\end{document}

평소와 마찬가지로 nicematrix여러 가지 컴파일이 필요합니다.

위 코드의 출력

관련 정보