2

Why is this red box overlay not anchoring properly to the rectangle headerbox?

  • Don't use xshift or yshift.
  • I want that headerbox against the topmargin, now there is white space between them.
  • Don't change "baseline=(headerbox.center)" in my headerbox description (so my tekst is centered. Use by example "\node at (headerbox.north east)..." to anchor the red box to it.

Blockquote

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm,showframe]{geometry}
\begin{document}
\newcommand{\newtab}[2]{%
    \begin{tikzpicture}[overlay]
        \node[inner sep=2mm,text=white] (#1) {#2};
        \begin{scope}[on background layer]
        \draw[fill=red]%
        ($(#1.north east)$)%
        --($(#1.north west)$)%
        --($(#1.south west)$)%
        -- ($(#1.south east)$)%
        -- cycle;%
        \end{scope}%
        \end{tikzpicture}% 
}%
\noindent\begin{tikzpicture}[rounded corners=0mm, outer sep=0pt,baseline=(headerbox.center)]%
\path node[rectangle,minimum width=\textwidth-\fboxrule,minimum height=3cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in](headerbox)%
{Headertekst}%
;%
\node at (headerbox.north east)[anchor=south east,inner sep=0pt, outer sep=0pt, line width=0cm] {\newtab{headerbox}{Labeltekst}};
\end{tikzpicture}\par Test%
\end{document}

Blockquoteenter image description here

  • 1
    You don't need to maintain that baseline to centre the text. You do need not to maintain it to get the vertical alignment you want. – cfr Feb 19 '16 at 4:04
1

Add an adjustment down for half the line width if that matters in your real document. I don't see you need all the libraries here, but still.

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,backgrounds}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm,showframe]{geometry}
\begin{document}
  \newcommand{\newtab}[2]{%
    \begin{tikzpicture}[overlay]
      \node[inner sep=2mm,text=white] (#1) {#2};
      \begin{scope}[on background layer]
        \draw[fill=red]%
        ($(#1.north east)$)%
        --($(#1.north west)$)%
        --($(#1.south west)$)%
        -- ($(#1.south east)$)%
        -- cycle;%
      \end{scope}%
    \end{tikzpicture}%
  }%
  \noindent\begin{tikzpicture}[rounded corners=0mm, outer sep=0pt]%
    \path node[rectangle,minimum width=\textwidth-\fboxrule,minimum height=3cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in, anchor=north](headerbox) at (current page.north)
    {Headertekst}%
    ;%
    \node at (headerbox.north east)[anchor=south east,inner sep=0pt, outer sep=0pt, line width=0cm] {\newtab{headerbox}{Labeltekst}};
  \end{tikzpicture}\par Test%
\end{document}

tab

The thickness of the line is partly an artefact and partly because half of the line width is above the line drawing the frame around the text area, I think.

  • Agreed. I ment the centered baseline is for vertically centering this box to another box (that is not shown in my code or picture). – Roger123 Feb 19 '16 at 4:17
  • I want the ability to pop-up that label or not. Adjustining vertically and horizontally makes the header wobbely when I pop the label. – Roger123 Feb 19 '16 at 4:25
  • I guess I'm not sure what you're trying to do. This isn't being created as a header at all. It is just the first thing on the page apart from the header ... – cfr Feb 19 '16 at 13:17
  • Here students have to put there names in. Just want to overlay the box, but it's odd... – Roger123 Feb 19 '16 at 17:03
0

I found a solution:

  • "Cleaner" code
  • \node[overlay], overlay is important at that node, otherwise the node takes space and the headerbox will move down.

Without that (headerbox) in node at (headerbox.north east)(headerbox) {Tekst}; the red box doesn't show. Don't know why cause (headerbox.north east) specifies clearly where I want to put the red box. Anyway, this code works fine:

Blockquote

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc,backgrounds}
\usepackage[showframe]{geometry}
\begin{document}
\noindent\begin{tikzpicture}
\node at (0,0) [rectangle,minimum width=\textwidth-\fboxrule,minimum height=5cm,draw=black,fill=white,inner xsep=0mm,inner ysep=0in,rounded corners=0mm, outer sep=0pt,baseline=(headerbox.center)](headerbox){};%
\node[overlay,inner sep=2mm,text=white,anchor=south east] at (headerbox.north east)(headerbox){Tekst};%
\begin{scope}[on background layer]%
\draw[fill=red,overlay]%
($ (headerbox.north east) $)
--($ (headerbox.north west) $)
--($ (headerbox.south west) $)
--($ (headerbox.south east) $)
-- cycle;%
\end{scope};%
\end{tikzpicture}%
\end{document}

Blockquote

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.