3

In the example below, I am trying to get identical blue and red boxes, without any text in the red one. I would like to have the red box exactly identical to the blue one, whose size depends on the content of the parbox, but without inserting any text in it? Is this possible?

\documentclass{book} 
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[anchor=north] (current page.north) node [fill=red,inner sep=1cm]{\parbox[c][][t]{\paperwidth}{\vphantom{Title}}};
\draw[anchor=north] (current page.north) node [fill=blue,opacity=.5,inner sep=1cm]{\parbox[c][][t]{\paperwidth}{Title}};
\end{tikzpicture}
\end{document}

enter image description here

The issue not explained here in details is that I am using a blend mode of Tikz which affects the text:

\draw[anchor=north] (midpoint) node [fill=black!75!ocre,blend mode=color,text width=\paperwidth,inner sep=1cm]{\parbox[c][][t]{\paperwidth}{\color{white} Text}};

and I want to keep the text as white. One solution is to add the text later in a different node.

  • You can use fit library to build a red box which exactly fits the blue one – Ignasi Feb 6 '15 at 14:24
  • Could you explain why do you want it? With this example, just a box with "correct" color would be enough. – Ignasi Feb 6 '15 at 14:59
4

No need for extra libraries nor big modification to your code; you're code will work as soon as TeX sees something in the \parbox such as a box of zero width (since the requirement in the question was to have "no text" in the box):

\documentclass{book} 
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[anchor=north] (current page.north) node [fill=red,inner sep=1cm]{\parbox[c][][t]{\paperwidth}{\mbox{}\vphantom{Title}}};
\draw[anchor=north] (current page.north) node [fill=blue,opacity=.5,inner sep=1cm]{\parbox[c][][t]{\paperwidth}{Title}};
\end{tikzpicture}
\end{document}

enter image description here

As percusse mentioned in his comment you could even dispense with the use of the \parboxes and give the desired width using text width:

\documentclass{book} 
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[anchor=north] (current page.north) node [fill=red,inner sep=1cm,text width=\paperwidth]{\vphantom{Title}};
\draw[anchor=north] (current page.north) node [fill=blue,opacity=.5,inner sep=1cm,text width=\paperwidth]{Title};
\end{tikzpicture}

\end{document}

The problem with your original code is that the \vphantom gives you the necessary height but the \parbox will still be empty. Adding the \mbox{} allows to set the specified width.

| improve this answer | |
  • Still why parbox instead of a node with inner sep? – percusse Feb 6 '15 at 16:31
  • @percusse why, indeed? I've updated my answer. Thanks for your suggestion. – Gonzalo Medina Feb 6 '15 at 16:40
  • @percusse but, then one might ask, why TikZ at all for doing this? :) Since the actual intent wasn't clear in the question, it's hard to tell which approach is the best one here. – Gonzalo Medina Feb 6 '15 at 16:42
  • @GonzaloMedina with percusse's suggestion, I think you can delete \mbox{}, with \vphantom is enough. You need \mbox only with \parbox. – Ignasi Feb 6 '15 at 16:44
  • @Ignasi Yes, you're right. Box deleted. Thanks. – Gonzalo Medina Feb 6 '15 at 17:15
4

You can use let operation (needs calc library)

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node [fill=blue,inner sep=1cm,text width=0.5\paperwidth,anchor=north](a) at (0,0){Title};
\draw
 let \p1 = ($ (a.east) - (a.west) $),
     \n1 = {veclen(\x1,\y1)},
     \p2 = ($ (a.south) - (a.north) $),
     \n2 = {veclen(\x2,\y2)}
in
node [fill=red,inner sep=1cm,minimum width=\n1,minimum height=\n2,anchor=north,opacity=.5] at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here

As suggested by Ignasi, you can use fit library too (easier)

\documentclass[border=4mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\node [fill=blue,inner sep=1cm,text width=0.5\paperwidth](a) at (0,0){Title};
\node [fit=(a),fill=red,inner sep=2pt,opacity=0.5]  {};
\end{tikzpicture}
\end{document}

enter image description here

inner sep=2pt, in the red node is just for demonstration and make it 0pt to fit exactly.

| improve this answer | |
  • Interesting but not acceptable as you draw the red box after the blue one: I want the red one before the blue one, even though we know the size of the blue one only. – pluton Feb 7 '15 at 5:05
  • @pluton I don't have a crystal ball. – user11232 Feb 7 '15 at 5:07
  • :) really too bad! Just get one. – pluton Feb 7 '15 at 5:17
  • @pluton: Really I don't know your aim. If you fix the width of blue box say at \paperwidth using text width =\paperwidth, then you can draw the red one before blue using the same text width=\paperwidth. Why do you want to use an explicit \parbox? It is not the tikz way, text width =\paperwidth is. – user11232 Feb 7 '15 at 5:19
  • @pluton Only if you tell me from where ;-) – user11232 Feb 7 '15 at 5:20

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.