테두리 색상이 다른 라텍스 맞춤형 테이블

테두리 색상이 다른 라텍스 맞춤형 테이블

저는 이 특정 테이블에 대한 코드를 Latex(바로 위 이미지)로 작성하고 싶었습니다. 표 테두리의 너비, 표 테두리의 색상 및 두 셀 사이의 공백을 정의하고 싶습니다. 또한 텍스트가 가로 및 세로 중앙에 배치되기를 원했습니다.

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

나는 다음과 같은 것을 시도했습니다.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx, graphicx}


\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}

\begin{document}

\setlength{\arrayrulewidth}{1mm}
\renewcommand{\arraystretch}{2}

\noindent
\begin{table}[h!]
    \centering
    \begin{tabular}{cc}
        \arrayrulecolor{green}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
         \color{green} GREEN \\
         \hline
        \end{tabular}
    & 
        \arrayrulecolor{blue}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
             \color{blue} BLUE \\
        \hline
            \end{tabular}
    \end{tabular}
\end{table}



\end{document}

코드를 편집했는데 작동하지만 억제할 수 없다는 경고가 표시됩니다. "\hline 명령을 잘못된 위치, 아마도 테이블 외부에서 사용했습니다. \hline 명령이 테이블 내부에 작성된 경우 다음을 시도하십시오. 그 앞에 \를 포함합니다."

표 내부의 테두리 색상을 변경할 수 없는 것 같습니다.

답변1

이를 위해 반드시 테이블이 필요한 것은 아닙니다. 두 개의 상자를 원하는 크기의 공백을 두고 가로로 나란히 배치할 수 있습니다.

\documentclass{article}
\usepackage[svgnames]{xcolor}

\begin{document}
{%
  \centering
  \setlength\fboxrule{1mm}   % Line width 
  \setlength\fboxsep{6pt}    % Padding       
  \fcolorbox{Green}{Green!5}{%  or white instead of the tint Green!5
    \parbox{4cm}{\centering\textcolor{Green}{GREEN}}}%
  \hspace{2em}%A
  \fcolorbox{Blue}{Blue!5}{%
    \parbox{4cm}{\centering\textcolor{Blue}{BLUE}}}
\par}
\end{document}

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


또는 다음을 사용할 수 있습니다.틱즈예를 들어 둥근 모서리와 같은 몇 가지 옵션이 더 있습니다. 그러나 추가 모듈이 포함된 거대한 패키지인 를 배워야 하며 tikz처음에는 조금 무서울 수도 있습니다.

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand\boxtext{Sample sample sample sample sample sample sample}

\tikzset{
  box/.style = {
    draw,
    line width = 1mm,
    color=#1, fill=#1!5,
    align=center, text width=4cm, font=\normalfont,
    inner sep=6pt+1mm, outer sep=0pt,
    rounded corners,
  },
}


\begin{document}
{%
  \centering
  \tikz[node distance = 2em] {
    \node (G) [box=Green] {\boxtext};
    \node (B) [box=Blue, right=of G] {\boxtext};}
  \par}
\end{document}

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

답변2

해결 방법으로 기본 테이블 형식의 두 번째 셀에 있는 작은 테이블 형식에 를 {NiceTabular}사용할 수 있습니다. nicematrix환경에는 {NiceTabular}규칙 색상에 대한 자체 키가 있습니다.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx, graphicx}
\usepackage{nicematrix}


\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}

\begin{document}

\setlength{\arrayrulewidth}{1mm}
\renewcommand{\arraystretch}{2}

\noindent
\begin{table}[h!]
    \centering
    \begin{tabular}{cc}
        \arrayrulecolor{green}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
         \color{green} GREEN \\
         \hline
        \end{tabular}
    & 
        \begin{NiceTabular}{|>{\centering\arraybackslash}m{5cm}|}[rules/color=blue]
        \hline
             \color{blue} BLUE \\
        \hline
        \end{NiceTabular}
    \end{tabular}
\end{table}

\end{document}

여러 컴파일이 필요합니다( nicematrix내부적으로 PGF/Tikz 노드를 사용하기 때문에).

위 코드의 출력

답변3

다음은 hhline과 colortbl을 사용한 솔루션입니다.

\documentclass{article}
\usepackage{hhline,colortbl}

%New column type so that text is centered
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

%Custom colors if needed
\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}


\begin{document}

\setlength\arrayrulewidth{2pt} % thickness of table lines
\renewcommand{\arraystretch}{2}  % horizontal spacing

%create a new hline called myhline with the colors needed
\newcommand\myhline[1]{%
\hhline{%
>{\arrayrulecolor {green}}%
#1|-% solid green colored line
>{}%
#1|~%  no line
>{\arrayrulecolor{blue}}%
|-#1%  solid blue colored line
}}


% start array
\begin{tabular}{
 !{\color{green}\vrule width 2pt } % vertical line color, line width
 M{5cm}  % custom column type, column width
 !{\color{green}\vrule width 2pt} 
 M{1cm}
 !{\color{blue}\vrule width 2pt}
 M{5cm}
 !{\color{blue}\vrule width 2pt}
}


\myhline{}% Draw custom line
\color{green} GREEN& &\color{blue} BLUE\\
\myhline{}% Draw custom line
\end{tabular}


\end{document}


결과는 다음과 같습니다. 여기에 이미지 설명을 입력하세요

참고자료:

Colortbl 매뉴얼/가이드

Hhline 매뉴얼/가이드

테이블의 열 값을 중앙에 배치하는 방법은 무엇입니까?

답변4

해결책 tabularray이 없습니다:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
\begin{table}[h!]
    \centering
    \begin{tblr}{colspec={Q[c,m,5cm, fg=green]cQ[c,m,5cm, fg=blue]},
        hline{1-2}={1}{green, wd=1mm},
        vline{1-2}={green, wd=1mm},
        hline{1-2}={3}{blue, wd=1mm},
        vline{3-4}={blue, wd=1mm},
        rows={ht=1cm},stretch=0
        }
        GREEN && BLUE \\
    \end{tblr}
\end{table}
\end{document}

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

관련 정보