2

This is a follow-up question of Set table background hatched and shaded using tikz. I want to specify the height of the orange cell as "number of lines" like it is done for the \ShadeCell[5] command (5 means "fill 6 lines"). I want to replace "-- space for 6 lines --" by something which reserves space for 6 normal text lines. How can I do this?

\documentclass[10pt]{article}
\usepackage[margin=2cm]{geometry} % just for the example 
\usepackage[table]{xcolor} 
\usepackage{array}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{calc,shadings,patterns}

% Andrew Stacey's code from
% https://tex.stackexchange.com/a/50054/3954
\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] {#1;}}
\makeatother
% end of Andrew's code


\newcommand\ShadeCell[4][0pt]{%
\begin{tikzpicture}[remember picture,overlay] %
\draw [fill=orange,orange]  ( $ (pic cs:#2) + (0pt,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (-0.4pt,-#1*\baselineskip-.8ex) $ ); 
\end{tikzpicture}
}%


\newcommand\Text{Quisque ullamcorper placerat ipsum. Cras nibh. Lorem ipsum dolor sit amet}

\begin{document}

\ShadeCell[5]{start4}{end4}{top color=gray!40}

\noindent\begin{tabularx}{0.6\textwidth}{| X | X | X |}
\hline
\Text & \multicolumn{1}{!{\hspace*{-0.4pt}\vrule\tikzmark{start4}}X!{\vrule\tikzmark{end4}}}{-- space for 6 lines --} & \Text \\
\hline
\end{tabularx}

\end{document}
  • You could try 6\baselineskip, or \newlength{mydim}\settolheight{\mydim}{\parbox{1in}{|\\|\\|\\|\\|\\|}} – John Kormylo Dec 8 '13 at 16:41
  • @JohnKormylo: Mmmhhh, doesn't work. In both cases I am getting tons of Latex errors... – user4811 Dec 8 '13 at 18:13
  • I thought you were using a Tikz cell array, not tabularx. Never mind. – John Kormylo Dec 9 '13 at 2:05
3

Here is a much simpler solution.

\documentclass[10pt]{article}
\usepackage[margin=2cm]{geometry} % just for the example 
\usepackage{tikz}

\newcommand\Text{Quisque ullamcorper placerat ipsum. Cras nibh. Lorem ipsum dolor sit amet}

\newlength{\cellW}% cell width (inner)
\setlength{\cellW}{0.2\textwidth}
% outer width = inner width + .6666em (initial)

\newlength{\cellR}% raisebox (cell height - \baselineskip)
\setlength{\cellR}{5\baselineskip}

\newcommand{\cell}[2]% #1 = fill color, #2 = text
{\node[draw=black,fill=#1]
 {\raisebox{\cellR}{\parbox[t]{\cellW}{\raggedright #2}}};}

\begin{document}

\noindent
\begin{tikzpicture}
 \matrix{
  \cell{white}{\Text}&
  \cell{orange}{-- Room for six lines --}&
  \cell{white}{\Text}\\
 };
\end{tikzpicture}

\end{document}

matrix

| improve this answer | |
  • You can also use \cell{color}{\centering{text}}. – John Kormylo Dec 9 '13 at 16:08
2

You can enter a "widthless" box with height equal to 6 lines to that cell:

\rule{0pt}{6\baselineskip}

By default, however, the bottom of the box is aligned with the baseline of the first line. To top-align the box, one can use the adjustbox package:

\adjustbox{valign=t}{\rule{0pt}{6\baselineskip}}

Full Code

\documentclass[10pt]{article}
\usepackage[margin=2cm]{geometry} % just for the example 
\usepackage[table]{xcolor} 
\usepackage{array}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{calc,shadings,patterns}
\usepackage{adjustbox} % loaded to adjust alignment of box

% Andrew Stacey's code from
% http://tex.stackexchange.com/a/50054/3954
\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] {#1;}}
\makeatother
% end of Andrew's code


\newcommand\ShadeCell[4][0pt]{%
\begin{tikzpicture}[remember picture,overlay] %
\draw [fill=orange,orange]  ( $ (pic cs:#2) + (0pt,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (-0.4pt,-#1*\baselineskip-.8ex) $ ); 
\end{tikzpicture}
}%


\newcommand\Text{Quisque ullamcorper placerat ipsum. Cras nibh. Lorem ipsum dolor sit amet}

% macro to save typing. NB: to "hide" the box, change 5pt to 0pt
\newcommand\sixlines{\adjustbox{valign=t}{\rule{5pt}{6\baselineskip}}}

\begin{document}

\ShadeCell[5]{start4}{end4}{top color=gray!40}

\noindent\begin{tabularx}{0.6\textwidth}{| X | X | X |}
\hline
\Text & \multicolumn{1}{!{\hspace*{-0.4pt}\vrule\tikzmark{start4}}X!{\vrule\tikzmark{end4}}}{\sixlines} & \Text \\
\hline
\end{tabularx}

\end{document}

Output

enter image description here

| improve this answer | |
  • Thx! I have voted for John's solution because it is a little bit simpler. If I could I would vote for both solutions. – user4811 Dec 9 '13 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.