Graphics, Figures & TablesQuestions about TikZ with position ("at" and "anchor")

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Akexandre
Posts: 9
Joined: Thu Mar 26, 2020 3:31 am

Questions about TikZ with position ("at" and "anchor")

Post by Akexandre »

Hello guys

I'm requesting your help because after trying to understand this according to TikZ handbook and many example on internet, I feel lost.

I would like a fancy header with Picture (photo) with a rectangle on top of the page width inside a photo left center inside the box and Name center inside the box.

First of all, the code (I tried to make it as simple as possible):

Code: Select all

\documentclass[]{report}
\usepackage[a4paper, top= 1cm, left=1.5cm, right=1.5cm]{geometry}
\usepackage{tikz}
\usepackage{lipsum}

\newcommand{\heading}[2]{
  \begin{tikzpicture}[remember picture,overlay]
    \node [rectangle, fill=gray, anchor=north, minimum width=\paperwidth, minimum height=3cm] 
    (box) at (current page.north);
    \node[anchor=center] at (box) {\Huge\color{white}%
      {#1}\ {#2}};
    \node[anchor=north east, minimum size=1in] at (current page.north)
     {\includegraphics[scale=.2]{example-image-a}};
    \end{tikzpicture}
    \vskip 2cm
}

\begin{document}
    \heading{John}{Doe}
    \lipsum
    \lipsum
\end{document}
Here my questions :

Code: Select all

\node [rectangle, fill=gray, anchor=north, minimum width=\paperwidth, minimum height=3cm] 
 (box) at (current page.north);
- I'm really confusing "anchor=north" and "at (current page.north)" : I would like to have an explanation if posible
- (box) : what's the meaning of this ? I was expecting north/south/west/east values (of box here means "generic define" ? )

Code: Select all

\node[anchor=north east, minimum size=1in] at (current page.north) 
 {\includegraphics[scale=.2]{example-image-a}};
- I tried to play with this because I want the sample picture on the left / top of the page (like CV picture). I tried to change current page.north with "box" and it appears below the first node. I tried many manipulation, but it's always doing thing I do not get (I think it's clearly related to my comprehension of anchor VS at)

I think I do not have the understanding yet, that's why I ask you help as expert to help me.

Thank you very much for helping me.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Akexandre
Posts: 9
Joined: Thu Mar 26, 2020 3:31 am

Questions about TikZ with position ("at" and "anchor")

Post by Akexandre »

I try again to request your help because I did not have an answer from community and I'm stuck from the first post.

Even a small hint could be very helpful for me :)

Thank you very much for helping me - I would be very nice ;)
Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Questions about TikZ with position ("at" and "anchor")

Post by Bartman »

Your first node is missing the braces of the required argument. Without the braces you should get an error message.

Should this heading only be on the first page or on every page?

If you only want to insert it on the first page, you can do something like that:

Code: Select all

\documentclass{report}
\usepackage[a4paper, top=1cm, hmargin=1.5cm]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
     
\newcommand{\heading}[2]{
    \begin{tikzpicture}[remember picture, overlay]
        \node [
            fill=gray, 
            anchor=north,
            minimum width=\paperwidth, 
            minimum height=3cm
        ] (box) at (current page.north) [font=\Huge, text=white] {#1 #2};
        \path 
            (box.west) -- 
                node {\includegraphics[scale=.2]{example-image-a}} 
            (box.center)
        ;
    \end{tikzpicture}
    \vskip 2cm
}
     
\begin{document}
\heading{John}{Doe}
\lipsum
\lipsum
\end{document}
Akexandre
Posts: 9
Joined: Thu Mar 26, 2020 3:31 am

Questions about TikZ with position ("at" and "anchor")

Post by Akexandre »

Thank you for your answer Bartman but it's increasing my comprehension (Path / box.west / box.center) but thx for missing braces - realy good point :)

But I woud like help on my original questions to take the benefits on this instead of having a new example adding complexicity (even if it sounds to reach the point I want to).

Here my questions :
- I'm really confusing "anchor=north" and "at (current page.north)" : I would like to have an explanation if posible
- (box) : what's the meaning of this ? I was expecting north/south/west/east values (of box here means "generic define" ? )
- I tried to play with this because I want the sample picture on the left / top of the page (like CV picture). I tried to change current page.north with "box" and it appears below the first node. I tried many manipulation, but it's always doing thing I do not get (I think it's clearly related to my comprehension of anchor VS at)

- - - -
Maybe it's to complexe to explain by answering to me but I try.
If you have links or example, it could be also enough.

Let me know If I'm making this too complex or If I'm missing somehting obvious ?

Many thx for your answer :)
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Questions about TikZ with position ("at" and "anchor")

Post by rais »

Well,
\node[anchor=north] (box) at (current page.north) {..};
creates a node called `box' at pgf/tikz special node `current page' to the north.
In other words, the new `box' node is to be created at the top of the (current) page.
To prevent the contents of this new node from spilling over the top of the page, it uses `anchor=north' to align the top of the new node to the top of the page.
If you'd put `anchor=south' here, the new node would be above the current page.
IIRC, the default for anchor would be center, so placing a node at the top of the page without such an anchor=north option would lead to (the upper) half of its contents disappearing.
The meaning of naming a node (box) is simply so you can refer to this node (or specific areas of it like `box.west') later on, as Bartman already demonstrated (among other things).
Why the originator chose `box' as name for the node I cannot say, but (s)he could have named it something else without trouble.

KR
Rainer
Akexandre
Posts: 9
Joined: Thu Mar 26, 2020 3:31 am

Questions about TikZ with position ("at" and "anchor")

Post by Akexandre »

Thank you Rais for your answer

Indeed it makes things clearer - I have to admit that the logic behind is "somehow" strange in compare to other GUI language

I'm going to play with your explanation and try to move forward.

Thx for your help, it was excatly what I was looking for.

Have a very good day (evening)

8-)
Akexandre
Posts: 9
Joined: Thu Mar 26, 2020 3:31 am

Questions about TikZ with position ("at" and "anchor")

Post by Akexandre »

For other people, I found this code on internet which is explaining easily the logic about anchor and at

Code: Select all

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.arrows}
\definecolor{myblue}{rgb}{0.29,0.5,0.74}
\definecolor{myred}{rgb}{0.73,0.32,0.3}
\definecolor{mygreen}{rgb}{0.59,0.73,0.34}
\definecolor{myviolet}{rgb}{0.49,0.39,0.62}
\definecolor{myturquoise}{rgb}{0.28,0.67,0.76}
\definecolor{myorange}{rgb}{0.97,0.58,0.27}

\begin{document}
\begin{tikzpicture}[myarrow/.style={single arrow,single arrow head extend=.1cm,anchor=west,
                                                       minimum height=.8cm,minimum width=.8cm},
    mycircle/.style={circle,text=white,inner sep=.2cm,outer sep=.2cm,minimum width=2.5cm}]
\node(a)[draw=myblue,mycircle,fill=myblue,circle,text=white,
              inner sep=.2cm,outer sep=.2cm]{Discipline};
\node(b)[draw=myturquoise,fill=myturquoise,myarrow,rotate=220]at(a.220){};
\node(c)[draw=myviolet,fill=myviolet,myarrow,rotate=320]at(a.320){};
\node(d)[draw=mygreen,fill=mygreen,myarrow,rotate=30]at(a.30){};
\node(e)[draw=myorange,fill=myorange,myarrow,rotate=150]at(a.150){};
\node(f)[draw=myred,fill=myred,myarrow,rotate=90]at(a.90){};
\node(g)[draw=myred,fill=myred,mycircle,anchor=south]at(f.east){Self Control};
\node(g)[draw=myorange,fill=myorange,mycircle,anchor=-40]at(e.east){Love};
\node(g)[draw=myviolet,fill=myviolet,mycircle,anchor=140]at(c.east){Obedience};
\node(g)[draw=mygreen,fill=mygreen,mycircle,anchor=-140]at(d.east){Trust};
\node(g)[draw=myturquoise,fill=myturquoise,mycircle,anchor=40]at(b.east){Journey};
\end{tikzpicture}
\end{document}
Inside this one, we have all the things : moving object according to referential object / difference with anchor and at / even the introduction to .style

Here the full thread : https://tex.stackexchange.com/questions ... -in-beamer
Post Reply