3

In the following code, I want the height of the yellow box to be increased automatically when the number of its lines gets more. Is there a way to do this?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[overlay,remember picture]
\fill[yellow]([yshift=-4cm]current page.north west) rectangle
  ([yshift=-5cm]current page.north east) node[pos=.5, red,align=center] { Test\\ Foo\\Bar};
\end{tikzpicture}
\end{document}
6

here is a solution

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[overlay,remember picture]
\node[fill=yellow, text=red, minimum width=\paperwidth, left, align=center, yshift=-4cm] 
at (current page.north east){ Test\\ Foo\\Bar};
\end{tikzpicture}
\end{document}
  • Is there a way to always preserve the space of the yellow box from the above edge of the page? I want the distance of the yellow box from the above edge of the page to be always, say, 3cm. – Sisabe Mar 19 '15 at 19:59
  • add anchor=north east to the node options and then set yshift to any distance you wish (or anchor=north in Ryan's solution) – summer Mar 19 '15 at 20:06
5

Instead of drawing a rectangle, just use a single tikz node:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[overlay,remember picture]
    \node
        [fill=yellow,yshift=-4cm,align=center,text width=\paperwidth] 
        at (current page.north) { Test\\ Foo\\Bar};
    \end{tikzpicture}
\end{document}

Using only one node and one position also makes xshift and yshiftmake more sense, making it easier to set the distance from the top of the page.

This allows TikZ to do all your sizing for you, and simplifies the code to keep it legible and maintainable.

enter image description here

5

Update: The solutions provided by touhami and Ryan are much simpler (thus much better) in this case. I leave this answer just in case it could help someone with other similar issues.


Check if this solution has the features you are looking for. The idea is to first define an auxiliar node T with the text that you are going to display. That auxiliar node allows to define the upper left corner of the yellow rectangle as

[yshift=5pt]current page.west |- T.north

that is, the intersection between the horizontal line 5pt above the text and the left edge of the page (and similarly for the lower right corner). I set the text in the auxiliar node white, but it would be covered by the yellow rectangle anyway.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\mytext{Test\\foo\\bar\\baz\\foofoo}
\begin{tikzpicture}[overlay,remember picture]
\node[align=center,white] (T) at (0,0) {\mytext};
\fill[yellow]([yshift=5pt]current page.west |- T.north) 
 rectangle ([yshift=-5pt]current page.east |- T.south) 
 node[pos=.5, red,align=center] (T) {\mytext};
\end{tikzpicture}
\end{document}

enter image description here

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.