使用 tikz 自定义表格:提取列的宽度

使用 tikz 自定义表格:提取列的宽度

目前我使用这种自定义类型的表格(带有包tikz,,booktabsetoolbox

\def\head#1\\{\shead#1&\\}
\def\shead#1&#2\\{\color{white}#1\ifstrempty{#2}{\\}{&\shead#2\\}}

\newsavebox{\dataTableContent} % Box
\newenvironment{dataTable}[1] % \new environment
{%
\begin{lrbox}{\dataTableContent}%
\begin{tabular}{#1}\head}%
%
{%
\end{tabular}
\end{lrbox}
\begin{tikzpicture}
  \node [inner xsep=0pt] (tbl){\usebox{\dataTableContent}};
  \begin{pgfonlayer}{background}
    % table
    \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black](tbl.north east) rectangle (tbl.south west);
    % top line
    \draw[rounded corners=1pt,top color=gray!10!black,bottom color=gray!50!black,draw=black]%
    ($(tbl.north west)$) rectangle ($(tbl.north east)-(0,1.5\baselineskip)$);
    % bottom rule
    \draw[rounded corners=0.25pt,fill=gray,draw=black]%
    (tbl.south west) rectangle ($(tbl.south east)+(0,0)$);
  \end{pgfonlayer}
\end{tikzpicture}}

这个环境运行得很好。但现在,我想对列做同样的事情(第一列为带有文本的黑色列)。我该怎么做?这似乎更复杂,因为我需要第一列的宽度(这取决于其中单词的长度)。

所以我的问题是:如何才能自动获取一行的宽度而无需手动指定它)。

梅威瑟:

 \documentclass[a4paper]{standalone}

 \usepackage[utf8x]{inputenc} 
 \usepackage{tikz}
 \usetikzlibrary{backgrounds,calc} 
 \usepackage{booktabs,etoolbox}
 \usepackage[active,tightpage]{preview}
 \PreviewEnvironment{tikzpicture}

    \def\head#1\\{\shead#1&\\}
     \def\shead#1&#2\\{\color{white}#1\ifstrempty{#2}{\\}{&\shead#2\\}}

     \newsavebox{\dataTableContent} % Box
     \newenvironment{dataTable}[1] % \new environment
     {%
     \begin{lrbox}{\dataTableContent}%
     \begin{tabular}{#1}\head}%
     %
     {%
     \end{tabular}
     \end{lrbox}
     \begin{tikzpicture}
       \node [inner xsep=0pt] (tbl){\usebox{\dataTableContent}};
       \begin{pgfonlayer}{background}
         % table
         \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black](tbl.north east) rectangle (tbl.south west);
         % top line
         \draw[rounded corners=1pt,top color=gray!10!black,bottom color=gray!50!black,draw=black]%
         ($(tbl.north west)$) rectangle ($(tbl.north east)-(0,1.5\baselineskip)$);
         % bottom rule
         \draw[rounded corners=0.25pt,fill=gray,draw=black]%
         (tbl.south west) rectangle ($(tbl.south east)+(0,0)$);
       \end{pgfonlayer}
     \end{tikzpicture}}

 \begin{document}

 \begin{dataTable}{lcc} et&Rien&tout\\\midrule ou&si&non
 \end{dataTable}

 \end{document}

结果:

结果

解决方案(基于以下建议):

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{booktabs}
\usepackage{tikz}                   
\usetikzlibrary{calc,positioning,shapes,backgrounds,matrix}
%preview tikzpicture
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

%for checked and unchecked symbols
\usepackage{pifont}
\newcommand{\cmark}{\color{green!50!black}\ding{51}}%
\newcommand{\xmark}{\color{red}\ding{55}}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{dataTableV}[2] % \new environment
{%
 \begin{tikzpicture}
 \def\nbRow {#2}
\def\nbCol {#1}
 \matrix (m) [
      matrix of nodes,
      nodes={minimum height=2em,text width=2cm,align=center, anchor=center, outer sep=0pt},
      row sep=-\pgflinewidth,column sep=-\pgflinewidth,
      column 1/.style={align=left,text = white},
      ]}{;\begin{pgfonlayer}{background}
         % table
         \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black]
               (m-1-1.north west) rectangle (m-\nbRow-\nbCol.south east);
         \draw[rounded corners=1pt,rounded corners=1pt,top color=gray!10!black,
              bottom color=gray!80!black,draw=black] (m-1-1.north west) rectangle
                  (m-\nbRow-1.south east); 

       \end{pgfonlayer}
       \end{tikzpicture}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{dataTableVH}[2] % \new environment
{%
 \begin{tikzpicture}
 \newcommand*{\nbRow}{#2}%
 \newcommand*{\nbCol}{#1}%
\newcommand*{\ColorGradT}{10}%
\newcommand*{\ColorGradB}{80}%
\pgfmathsetmacro{\ColorVO}{\ColorGradT}
\pgfmathsetmacro{\ColorP}{(\ColorGradB-\ColorVO)/\nbRow}
\pgfmathsetmacro{\ColorGradM}{\ColorP*1+\ColorVO}%
   \matrix (m) [
      matrix of nodes,
      nodes={minimum height=2em,text width=2cm,align=center, anchor=center, outer sep=0pt},
      row sep=-\pgflinewidth,column sep=-\pgflinewidth,
      column 1/.style={align=left,text = white},
      row 1/.style={align=left,text = white},
      ]}{;\begin{pgfonlayer}{background}
         % table
         \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black] (m-1-1.north west) rectangle (m-\nbRow-\nbCol.south east);         
         \draw[rounded corners=1pt,rounded corners=1pt,top color=gray!\ColorGradT!black,bottom color=gray!\ColorGradB!black,draw=black] (m-1-1.north west) rectangle (m-\nbRow-1.south east); 
         \draw[rounded corners=1pt,rounded corners=1pt,top color=gray!\ColorGradT!black,bottom color=gray!\ColorGradM!black,draw=none] (m-1-1.north west) rectangle (m-1-\nbCol.south east);

       \end{pgfonlayer} 
       \end{tikzpicture}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\begin{document}
\begin{dataTableVH}{6}{6}{|[align=left]|&Face Plane&Sphère&Cylindre&Cône&Cercle\\
|[align=left]|Face Plane&\cmark&\cmark&\cmark&\xmark&\xmark\\
|[align=left]|Sphère&\cmark&\cmark&\xmark&\xmark&\xmark\\
|[align=left]|Cylindre&\cmark&\xmark&\cmark&\xmark&\xmark\\
|[align=left]|Cône&\xmark&\cmark&\xmark&\cmark&\cmark\\
|[align=left]|Cercle&\xmark&\cmark&\xmark&\cmark&\xmark\\
} 
\end{dataTableVH}
\begin{dataTableV}{6}{5}{
|[align=left]|Face Plane&\cmark&\cmark&\cmark&\xmark&\xmark\\
|[align=left]|Sphère&\cmark&\cmark&\xmark&\xmark&\xmark\\
|[align=left]|Cylindre&\cmark&\xmark&\cmark&\xmark&\xmark\\
|[align=left]|Cône&\xmark&\cmark&\xmark&\cmark&\cmark\\
|[align=left]|Cercle&\xmark&\cmark&\xmark&\cmark&\xmark\\} 
\end{dataTableV}
\end{document}

结果: 解析度1 解析度2

答案1

这只是一个想法,你必须在此基础上进行构建。我觉得你最好colortbl按照约翰的建议使用。但如果你想要渐变等,你可以matrix使用tikz

 \documentclass{standalone}
 \usepackage{tikz}
 \usetikzlibrary{backgrounds,matrix}

 \begin{document}
 \begin{tikzpicture}
 \matrix (m) [
      matrix of nodes,
      nodes={minimum height=2em,text width=2cm,align=center, anchor=center, outer sep=0pt},
      row sep=-\pgflinewidth,column sep=-\pgflinewidth,
      column 1/.style={nodes={align=left,text = white}},
      ]
{
  et & Rien & tout\\
  ou & si   & non\\
};

\begin{pgfonlayer}{background}
         % table
         \draw[rounded corners=1pt,top color=gray!1,bottom color=gray!30,draw=black]
               (m-1-1.north west) rectangle (m-2-3.south east);
         \draw[rounded corners=1pt,rounded corners=1pt,top color=gray!10!black,
              bottom color=gray!80!black,draw=black] (m-1-1.north west) rectangle
                  (m-2-1.south east);

       \end{pgfonlayer}
\end{tikzpicture}


 \end{document}

在此处输入图片描述

相关内容