使用tikz生成表格时单元格高度随文本变化的问题

使用tikz生成表格时单元格高度随文本变化的问题

我正在使用 tikz 生成表格,代码如下:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95}
\newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}
\usepackage{tikz}   % package for fancy tables
\usetikzlibrary{matrix}
\usepackage{varwidth}

\begin{document}
\tikzset{ 
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            text width=10em,
            rectangle,
            draw=black
        },
        %minimum height=1.5em,
        text centered
        text depth=1.5ex,
        text height=2.5ex,
        nodes in empty cells,
%%
        every even row/.style={
            nodes={fill=cyan!5}
        },
        column 1/.style={   % first column
            nodes={text width=30em}
        },
        row 1/.style={      % first row
            nodes={
                fill=white,
                text=black,
                align=center
                %font=\bfseries
            }
        }
    }
}

\begin{tikzpicture}%[every node/.style={anchor=base,text depth=1.5ex,text height=3ex,text width=1em}]

\matrix (first) [table,text width=6em]
{
Command & Description \\
\code{git init}   & Initialize a local Git repository  \\
\code{git clone ssh://[email protected]/[usrname]/[repo-name].git}  & Create a local copy of a remote repository  \\
};
\end{tikzpicture}

\end{document}

但是,我得到了如下所示的结果。除了单元格的高度随其中文本的高度而变化并且单元格的基线不对齐之外,一切都很好。我很难弄清楚这一点,我想知道如何解决这个问题。提前感谢您的时间,我非常感谢任何建议或分享的经验。 在此处输入图片描述

答案1

这是这个答案,现在你只需输入类似以下内容即可为整列或整行着色

full row 2/.style={fill=blue!20},

矩阵分隔符取自上面的帖子。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc,positioning,fit,backgrounds}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\tikzset{matrix vlines/.style={execute at end matrix={
\edef\tikzmatrixname{\tikz@fig@name}
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\xdef\FitList{}
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-col-\XX) {};
}
\path (\tikzmatrixname.west) coordinate (aux-1);
\foreach \XX in {2,...,\the\pgf@matrix@numberofcolumns}
{\path ($(\tikzmatrixname-col-\XX.west)!0.5!(\tikzmatrixname-col-\the\numexpr\XX-1\relax.east)$)
coordinate (aux-\XX);
\draw[#1]  (aux-\XX|-\tikzmatrixname.north)
 --  (aux-\XX|-\tikzmatrixname.south);
\begin{scope}[on background layer]
 \path[/tikz/full column \the\numexpr\XX-1\relax/.try] 
 (aux-\the\numexpr\XX-1\relax-|\tikzmatrixname.north) rectangle
 (aux-\XX-|\tikzmatrixname.south);
\end{scope}

}
}},matrix hlines/.style={execute at end matrix={
\edef\tikzmatrixname{\tikz@fig@name}
\foreach \YY in {1,...,\the\pgfmatrixcurrentrow}
{\xdef\FitList{}
\foreach \XX in {1,...,\the\pgf@matrix@numberofcolumns}
{\ifnodedefined{\tikzmatrixname-\YY-\XX}{\xdef\FitList{\FitList (\tikzmatrixname-\YY-\XX)}}{}
}
\node[fit=\FitList,draw=none,fill=none,inner sep=0pt,draw=none] (\tikzmatrixname-row-\YY) {};
}
\path (\tikzmatrixname.north) coordinate (aux-1);
\foreach \XX in {2,...,\the\pgfmatrixcurrentrow}
{\path ($(\tikzmatrixname-row-\XX)!0.5!(\tikzmatrixname-row-\the\numexpr\XX-1\relax)$)
coordinate (aux-\XX);
\draw[#1]  (aux-\XX) (aux-\XX-|\tikzmatrixname.west)
 --  (aux-\XX-|\tikzmatrixname.east);
\begin{scope}[on background layer]
 \path[/tikz/full row \the\numexpr\XX-1\relax/.try] (aux-\the\numexpr\XX-1\relax-|\tikzmatrixname.west) rectangle
 (aux-\XX-|\tikzmatrixname.east);
\end{scope}
}
}},
matrix dividers/.style={matrix vlines=#1,matrix hlines=#1},
matrix frame/.style={execute at end matrix={
\draw[#1] (\tikz@[email protected] west) rectangle (\tikz@[email protected] east);
}}}
\makeatother
\newcommand\code[1]{\colorbox{gray!10}{\texttt{#1}}}
\begin{document}
\begin{tikzpicture}%[every node/.style={anchor=base,text depth=1.5ex,text height=3ex,text width=1em}]

\matrix (first) [matrix of nodes,matrix dividers,matrix frame,
 nodes={align=left,text height=1.2em,anchor=center},
 full row 2/.style={fill=blue!20},
 column 2/.style={nodes={text width=10em}},
 column 1/.style={nodes={
  text width={1.2*width("git clone ssh://[email protected]/[usrname]/[repo-name].git")}}}] %[table,text width=6em]
{
Command & Description \\
\code{git init}   & Initialize a local Git repository  \\
\code{git clone ssh://[email protected]/[usrname]/[repo-name].git}  & Create a local copy of a remote repository  \\
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容