3

This is an addendum of a previous question: How to place a \draw near the page number using tikzpicture environment.


What I want

I would like to have the section text moved to the top of every page, and place an image next to it. Look at this:

What I want

Why I mention sections? Because when the document is finished I want to make a ToC at the beginning.

  • The image (svg file) is always the same.

For example:

  • The full document has 8 pages in total.
  • In page 1 there should be nothing.
  • In pages 2-4, the green rectangle should have: Section One: Text with the image next to it.
  • In pages 5-7, the green rectangle should have: Section Two: More text with the image next to it.
  • In page 8, the green rectangle should have: Bibliography with the image next to it.

and we should be able to produce a ToC with those 3 sections (two sections and the bibliography).

What I have done

Consider this MWE (taken from the accepted answer of the addendum):

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage[hidelinks]{hyperref}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newsavebox{\arrowL}
\newsavebox{\arrowR}
\sbox\arrowR{\begin{tikzpicture}
\draw[line width=1mm,white,line cap=round,line join=round] 
(0,0) -- (7pt,8pt) -- (0pt,16pt);
\end{tikzpicture}}
\sbox\arrowL{\begin{tikzpicture}
\draw[line width=1mm,white,line cap=round,line join=round] 
(0,0) -- (-7pt,8pt) -- (0pt,16pt);
\end{tikzpicture}}
\fancyhf{}
\rhead{
%\newcommand{\sec}[1]{%
\begin{tikzpicture}[remember picture,overlay]%
        \fill[green] (current page.north west) rectangle ++(\paperwidth,-1.5cm);%
        \node[text=white,anchor=west,xshift=4cm,yshift=-.75cm,font=\LARGE\bfseries] at (current page.north west) (a) {Section One: Text};%
\end{tikzpicture}%
%}
}
\rfoot{
    \begin{tikzpicture}[remember picture,overlay]
        \fill[black] (current page.south west) rectangle ++(\paperwidth,1.5cm)
        node[midway,align=center,font=\LARGE\bfseries,text=white,xscale=1.5] 
        (pageno-\number\value{page}) 
        {\thepage}; % From https://tex.stackexchange.com/a/443744/152550
        \ifnum\value{page}<\getpagerefnumber{LastPage}
         \path ([xshift=\paperwidth/4]pageno-\number\value{page}) 
            node{\hyperlink{page.\the\numexpr\value{page}+1}{\usebox\arrowR}};
        \fi
        \ifnum\value{page}>1
         \path ([xshift=-\paperwidth/4]pageno-\number\value{page}) 
            node{\hyperlink{page.\the\numexpr\value{page}-1}{\usebox\arrowL}};
        \fi % From https://tex.stackexchange.com/a/529698/152550
    \end{tikzpicture}
}

\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}

%\sec{Section One: Text}
\tikz\node[circle,draw,
           path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=2.5cm]{Example.png}
               };
           }]{}; % From https://tex.stackexchange.com/a/193558/152550

Example
\newpage 
Another example

\end{document}

MWE output

Problems:

  • I don't know how to use \newcommand[1]{\sec}{...} to have different sections on pages, where the argument is the name of the section.
  • The cropped image does not show on the 2.5cm circle (inside \begin{document}). (I have loaded graphicx and put \graphicspath. I tried with \includegraphics{Example.png} outside tikzpicture and it worked.)
2

One way is to use titlesec. That way you can use ordinary \section commands for that purpose. This has various advantages (table of contents, hyperlinks, ...). You then only need to type

\section{I am a cat}
Example
\newpage 
\section{You are a duck}
Another example

in the body of your document.

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage[hidelinks]{hyperref}
\usepackage{lastpage}
\usepackage{refcount}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newsavebox{\arrowL}
\newsavebox{\arrowR}
\sbox\arrowR{\begin{tikzpicture}
\draw[line width=1mm,white,line cap=round,line join=round] 
(0,0) -- (7pt,8pt) -- (0pt,16pt);
\end{tikzpicture}}
\sbox\arrowL{\begin{tikzpicture}
\draw[line width=1mm,white,line cap=round,line join=round] 
(0,0) -- (-7pt,8pt) -- (0pt,16pt);
\end{tikzpicture}}
\fancyhf{}
\titleformat{\section}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \fill[green] (current page.north west) rectangle 
  ([yshift=-1.5cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=4cm,yshift=-.75cm]current page.north west)
   {Section \thesection: #1};
   \clip ([xshift=2cm,yshift=-.75cm]current page.north west)
    circle[radius=1.24cm];
   \node at 
   ([xshift=2cm,yshift=-.75cm]current page.north west)
    {\includegraphics[width=2.5cm]{example-image-duck}};
         \end{tikzpicture}
}[]

% see https://tex.stackexchange.com/a/236778
\titleformat{name=\section,numberless}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \fill[green] (current page.north west) rectangle 
  ([yshift=-1.5cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=4cm,yshift=-.75cm]current page.north west)
   {#1};
   \clip ([xshift=2cm,yshift=-.75cm]current page.north west)
    circle[radius=1.24cm];
   \node at 
   ([xshift=2cm,yshift=-.75cm]current page.north west)
    {\includegraphics[width=2.5cm]{example-image-duck}};
\end{tikzpicture}
}[]


\rfoot{
    \begin{tikzpicture}[remember picture,overlay]
        \fill[black] (current page.south west) rectangle ++(\paperwidth,1.5cm)
        node[midway,align=center,font=\LARGE\bfseries,text=white,xscale=1.5] 
        (pageno-\number\value{page}) 
        {\thepage}; % From https://tex.stackexchange.com/a/443744/152550
        \ifnum\value{page}<\getpagerefnumber{LastPage}
         \path ([xshift=\paperwidth/4]pageno-\number\value{page}) 
            node{\hyperlink{page.\the\numexpr\value{page}+1}{\usebox\arrowR}};
        \fi
        \ifnum\value{page}>1
         \path ([xshift=-\paperwidth/4]pageno-\number\value{page}) 
            node{\hyperlink{page.\the\numexpr\value{page}-1}{\usebox\arrowL}};
        \fi % From https://tex.stackexchange.com/a/529698/152550
    \end{tikzpicture}
}

\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}
\section{I am a cat}
Example
\newpage 
\section{You are a duck}
Another example

\newpage 
\section*{Bibliography}
A book about ducks and seven books about marmots.

\end{document}

enter image description here

One can change the details of the implementation easily.

4
  • Of course one can adjust the details of the implementation in many ways. – user194703 Feb 23 '20 at 23:26
  • Thanks! This answer contemplate the possibility of adding a section that has no number? For example, a section called Bibliography. – manooooh Feb 23 '20 at 23:31
  • 1
    @manooooh Yes: tex.stackexchange.com/a/102120. You can define a format for a numberless section. – user194703 Feb 23 '20 at 23:35
  • 2
    @manooooh I added an explicit example. (I am pretty sure one can tune things up, e.g. add the page break to the definition of section and so on. I will be offline now for a bit but happy to add things of that sort later, if needed.) – user194703 Feb 23 '20 at 23:41

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.