不同邊框顏色的乳膠訂製桌子

不同邊框顏色的乳膠訂製桌子

我想在 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}

在此輸入影像描述

相關內容