3

I am using tikz to generate a table with the following code:

\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://git@github.com/[usrname]/[repo-name].git}  & Create a local copy of a remote repository  \\
};
\end{tikzpicture}

\end{document}

However, I got the result as shown below. Everything is fine except that the height of the cell varies with the height of the texts in it and the baselines of the cells are not aligned. I had a really hard time figuring this out and I'm wondering how I could solve the problem. Thank you for your time in advance and I appreciate any suggestions or experience shared. enter image description here

Jack
  • 57
  • 1
    Welcome to TeX.SE! It seems that simple table will serve better than draw it as TikZ picture. Please make your question more clear by extending your code fragment to complete small document, which we can test as it is. – Zarko Jun 28 '19 at 22:44
  • 1
    Test the following: \documentclass{article}\usepackage{geometry} \usepackage{ragged2e}\usepackage{booktabs,tabularx} \begin{document} \noindent\begin{tabularx}{\linewidth}{@{} >{\hsize=0.75\hsize\RaggedRight}X>{\hsize=0.25\hsize\RaggedRight}X@{}} \toprule Command & Description \\\midrule \verb+git init+ & Initialize a local Git repository \\\addlinespace \verb+git clone ssh://git@github.com/[usrname]/[repo-name].git+ & Create a local copy of a remote repository \\ \bottomrule \end{tabularx}\end{document}. I may give you idea for different approach. – Zarko Jun 28 '19 at 23:02
  • Hi Zarko, thank you so much for the reply! I just update the code so now you can test it. My hope was to align different cells appropriately while keeping all the lines and the alternating coloring. – Jack Jun 29 '19 at 04:07
  • Thanks for the answer! That is pretty close to what I want. The only thing is that I hope that the nodes can be centered and aligned vertically (while also aligned to the left horizontally). I've tried text centered and align=center, but both did not work. Could you suggest me how I can align the text to the center vertically? Also, I hope that I can use alternating colors for all the even rows. I've tried every even line./style={nodes={fill=cyan!5}}, but it still did not work. I'm wondering if there is a way to do this (without specifying each even row separately). Thanks a lot! – Jack Jun 29 '19 at 10:16

1 Answers1

3

This is an extension of this answer, which now allows you color a full column or row by simply saying something like

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

The matrix dividers are taken from the above post.

\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@fig@name.south west) rectangle (\tikz@fig@name.north 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://git@github.com/[usrname]/[repo-name].git")}}}] %[table,text width=6em]
{
Command & Description \\
\code{git init}   & Initialize a local Git repository  \\
\code{git clone ssh://git@github.com/[usrname]/[repo-name].git}  & Create a local copy of a remote repository  \\
};
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks for the answer! That is pretty close to what I want. The only thing is that I hope that the nodes can be centered and aligned vertically (while also aligned to the left horizontally). I've tried text centered and align=center, but both did not work. Could you suggest me how I can align the text to the center vertically? Also, I hope that I can use alternating colors for all the even rows. I've tried every even line./style={nodes={fill=cyan!5}}, but it still did not work. I'm wondering if there is a way to do this (without specifying each even row separately). Thanks a lot! – Jack Jun 29 '19 at 23:27
  • @Wei-TseWEHS7661 That's very easy: use nodes={align=left,text height=1.2em,anchor=center},, i.e. append anchor=center to nodes={align=left,text height=1.2em}. I changed the answer accordingly. –  Jun 29 '19 at 23:28
  • Thank you so much for your useful answer! Now it works perfectly. – Jack Jun 30 '19 at 07:18
  • 1
    Done! Sorry I was not familiar with the rules here since this was my first post. – Jack Jul 01 '19 at 14:10