2

This is a combination of two previous questions (with their respective answers):How to create a command for the sections with an svg next to it on the top of the page? and How to make a conversation with rectangle shapes using tikzpicture environment?


The problem

Please consider this MWE (it is long because I need to show the actual header and footer):

\documentclass{article}
\usepackage{blindtext}
\def\maxwidth{16cm}
\def\maxheight{20cm}

\usepackage[a4paper,margin=.25in,top=1cm,footskip=0.25in]{geometry}

\usepackage[export]{adjustbox} % Loads also graphicx
\graphicspath{ {./images/} }
\usepackage{varwidth}

% From https://tex.stackexchange.com/a/529773/152550
\newif\ifStart
\Startfalse
\newif\ifImage
\Imagefalse

\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning}

\tikzset{basic/.style={rounded corners=.2cm,inner sep=.3cm},
    basic L/.style={basic,align=left,fill=white,anchor=north west,
        font=\sffamily,outer sep=0pt},
    my callout L/.style={basic L,
        append after command={pic[fill=white]{callout corner L}}},
    basic R/.style={basic,align=right,fill=green!60!black,anchor=north east,
        font=\sffamily,outer sep=0pt},
    my callout R/.style={basic R,
        append after command={pic[fill=green!60!black]{callout corner R}}},
    pics/callout corner L/.style={code={
    \path[pic actions] ([xshift=3mm]\tikzlastnode.north west) -- ++ (-9mm,0) 
    -- ([yshift=-6mm]\tikzlastnode.north west);}},
    pics/callout corner R/.style={code={
    \path[pic actions] ([xshift=-3mm]\tikzlastnode.north east) -- ++ (9mm,0) 
    -- ([yshift=-6mm]\tikzlastnode.north east);}},
    exercise/.style={rounded corners=.2cm,inner sep=.25cm,align=right,
        fill=blue,anchor=north,font=\sffamily,text=white},
    whatsapp/.cd,vdist/.initial=3mm,hdist/.initial=6mm,
    start/.is if=Start,
    image/.code=\Imagetrue\tikzset{whatsapp/graphics pars/.cd,#1},
    graphics pars/.cd,scale/.initial=1 %add additional keys here

}
% Added varwidth to set a max width for each of the shapes. From https://tex.stackexchange.com/a/46479/152550
% Me
\newcommand{\Me}[2][]{\noindent\begin{tikzpicture}
\tikzset{whatsapp/.cd,#1}
\ifStart
    \path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) 
    node[my callout L,alias=tmp] {\ifImage
     \includegraphics[max height=\maxheight,max width=\maxwidth,
        scale=\pgfkeysvalueof{/tikz/whatsapp/graphics pars/scale}]{#2}%
    \else
    \begin{varwidth}{\maxwidth}#2\end{varwidth}%
    \fi};
\else
    \path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) 
    node[basic L,alias=tmp] {\ifImage
     \includegraphics[max height=\maxheight,max width=\maxwidth,
        scale=\pgfkeysvalueof{/tikz/whatsapp/graphics pars/scale}]{#2}%
    \else
    \begin{varwidth}{\maxwidth}#2\end{varwidth}%
    \fi};
\fi 
    \path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% You
\newcommand{\You}[2][]{\noindent\begin{tikzpicture}
\tikzset{whatsapp/.cd,#1}
\ifStart
    \path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) 
    node[my callout R,alias=tmp] {\ifImage
     \includegraphics[max height=\maxheight,max width=\maxwidth,
        scale=\pgfkeysvalueof{/tikz/whatsapp/graphics pars/scale}]{#2}%
    \else
    \begin{varwidth}{\maxwidth}#2\end{varwidth}%
    \fi};
\else
    \path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) 
    node[basic R,alias=tmp] {\ifImage
     \includegraphics[max height=\maxheight,max width=\maxwidth,
        scale=\pgfkeysvalueof{/tikz/whatsapp/graphics pars/scale}]{#2}%
    \else
    \begin{varwidth}{\maxwidth}#2\end{varwidth}%
    \fi};
\fi 
    \path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
\newcommand{\Exercise}[2][]{\noindent\begin{tikzpicture}
    \path (0,0) (\textwidth/2,0) node[exercise,alias=tmp,#1] {#2};
    \path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}


\usepackage[explicit]{titlesec}

\usepackage{eso-pic}
\AddToShipoutPictureBG{\includegraphics[width=\paperwidth,height=\paperheight]{example-image}}

%%%%%%%%%%%%%%%%%%%%

\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{}

% From https://tex.stackexchange.com/a/529755/152550
\titleformat{\section}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \fill[green] (current page.north west) rectangle 
  ([yshift=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {Section \thesection: #1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
         \end{tikzpicture}
}[]

% From 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=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {#1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
\end{tikzpicture}
}[\vspace{-1cm}]

\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}

\Me[start]{\blindtext[1]\\ Text}

\Me[image={scale=0.75}]{example-image}

\You[start,image={scale=1}]{example-image-duck}

\Exercise{How many marmots?}

\You[start]{\blindtext[2]}

\Me[start]{\blindtext[1]}

\Me{\blindtext[2]}

\newpage

\section{You are a duck}

\Me{\blindtext[1]}

\newpage 
\section*{Bibliography}

\end{document}

This combined source code comes from @Schrödinger's cat who kindly made modifications once the answers of the cited questions were accepted.

Look at the output:

Collision with the header

There is a missing header. And sometimes it collides with the shapes.

Moreover, if we, for example, increase one image size, let's say we change \You[start,image={scale=1}]{example-image-duck} to \You[start,image={scale=1.35}]{example-image-duck}, there is a collision with the footer:

Collision with the footer

Question

Is there any way to prevent these collisions with the header and footer, preserving sizes? I don't care if one shape that does not fit in the page must go to the next page. I don't want collisions.

manooooh
  • 3,203
  • 2
  • 21
  • 46
  • 1
    How about \usepackage[a4paper,margin=.25in,top=2cm,bottom=2cm,footskip=0.25in]{geometry} or something like this, i.e. just increase the margins? –  Feb 25 '20 at 16:07
  • @Schrödinger'scat if I set those top and bottom values then the first page has a lot of space between the green and white shapes. In the second page, the section still missing: https://i.imgur.com/yXTGn0r.png I will try changing its value to obtain a better result, thank you! – manooooh Feb 25 '20 at 16:16
  • I guess this is partly my fault. In your previous question you were asking to create these shapes with TikZ, which is what my answer did. While writing this I was considering suggesting the tcolorbox package, which allows you to produce breakable variants of such shapes. Now that I see that some of them are enormous, I am tempted to suggest to redo them in tcolorbox to make them breakable. This would cure the problem of huge gaps when the last big shape does not fit on the page. –  Feb 25 '20 at 16:20
  • @Schrödinger'scat thanks. Using tcolorbox will be worth for image shapes, too? Because in my document I will mostly use images, some text and some sample code. – manooooh Feb 25 '20 at 16:27
  • I have not tested this link yet: https://tex.stackexchange.com/q/276358/152550 but can it make things easier between both header and footer collisions and shapes? – manooooh Feb 26 '20 at 13:24
  • @Schrödinger'scat do you have any approach? – manooooh Feb 28 '20 at 19:57
  • 1
    I'll try to convert those into tcolorboxes later. It should be very easy with frame code. –  Feb 28 '20 at 19:58

1 Answers1

1

This code translates the TikZ-based shapes into tcolorboxes. This has the benefit that they are breakable. so you waste less space. If you use the graphics key, the box will adjust its size to the content.

\documentclass{article}
\usepackage{lipsum}
\def\maxwidth{16cm}
\def\maxheight{20cm}

\usepackage[a4paper,margin=.25in,top=0.2cm,bottom=1.4cm,footskip=0.25in]{geometry}


% From https://tex.stackexchange.com/a/529773/152550
\newif\ifStart
\Startfalse
\newif\ifImage
\Imagefalse
\newif\ifCalloutRight
\CalloutRightfalse


\usepackage[skins,breakable]{tcolorbox}

\tcbset{start/.code={\Starttrue},callout right/.code={\CalloutRighttrue},
graphics/.code={\tcbset{whatsapp cont/.style={hbox}}},
whatsapp cont/.style={width=\maxwidth},
whatsapp/.style={empty,breakable,
left=2mm,right=2mm,top=2mm,bottom=2mm,fontupper=\sffamily,
whatsapp cont,after skip=1ex},
whatsap skin/.style={% code for unbroken boxes:
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0) 
    -- ([yshift=-6mm]frame.north east); 
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0) 
    -- ([yshift=-6mm]frame.north west); 
 \fi
\fi},
interior code={
\path[fill=#1,rounded corners=3mm] 
 (frame.south west) rectangle (frame.north east);
},  
% code for the first part of a break sequence: 
skin first is subskin of={emptyfirst}{%
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0) 
    -- ([yshift=-6mm]frame.north east); 
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0) 
    -- ([yshift=-6mm]frame.north west); 
 \fi
\fi},
interior code={
\path[fill=#1] 
 (frame.south west) [rounded corners=3mm] |-
 (frame.north)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},  
},
% code for the middle part of a break sequence: 
skin middle is subskin of={emptymiddle}{%
frame code={%
},
interior code={
\path[fill=#1] 
 (frame.south west) rectangle (frame.north east);
},  
},
% code for the last part of a break sequence: 
skin last is subskin of={emptylast}{%
frame code={%
},
interior code={
\path[fill=#1] 
 (frame.north west) [rounded corners=3mm] |-
 (frame.south)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},  
}}}

\newtcolorbox{Mebox}[1][]{#1,whatsapp,flush left,whatsap skin=white}
\newcommand{\Me}[2][]{\begin{Mebox}[#1]
#2
\end{Mebox}}


\newtcolorbox{Youbox}[1][]{#1,whatsapp,flush right,right skip=15mm,callout right,
    whatsap skin=green!60!black}
\newcommand{\You}[2][]{\begin{Youbox}[#1]
#2
\end{Youbox}}

\newtcolorbox{Exercisebox}[1][]{#1,whatsapp,coltext=white,center,whatsap skin=blue}
\newcommand{\Exercise}[2][]{\begin{Exercisebox}[#1]
#2
\end{Exercisebox}}


\usepackage[explicit]{titlesec}

\usepackage{eso-pic}
\AddToShipoutPictureBG{\includegraphics[width=\paperwidth,height=\paperheight]{example-image}}

%%%%%%%%%%%%%%%%%%%%

\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{}

% From https://tex.stackexchange.com/a/529755/152550
\titleformat{\section}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \path[left color=red!40!black,right color=red!60!black,middle color=red] 
  (current page.north west) rectangle 
  ([yshift=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {Section \thesection: #1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
         \end{tikzpicture}
}[\vspace{1cm}]

% From https://tex.stackexchange.com/a/236778
\titleformat{name=\section,numberless}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \path[left color=red!40!black,right color=red!60!black,middle color=red] 
  (current page.north west) rectangle 
  ([yshift=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {#1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
\end{tikzpicture}
}[\vspace{1cm}]

\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}

\Me[start]{\lipsum[1]\\ Text}

\Me[graphics]{\includegraphics[scale=0.75]{example-image}}

\You[start,graphics]{\includegraphics{example-image-duck}}

\Exercise{How many marmots?}

\You[start]{\lipsum[2]}

\Me[start]{\lipsum[1-3]}

\Me{\lipsum[4]}

\newpage

\section{You are a duck}

\Me{\lipsum[1]}

\You{\lipsum[2]}


\newpage 
\section*{Bibliography}

\end{document}

enter image description here

And this is a version in which the boxes shrink if they are smaller than \maxwidth i.e. you do no longer need the graphics key.

\documentclass{article}
\usepackage{lipsum}
\def\maxwidth{16cm}
\def\maxheight{20cm}

\usepackage[a4paper,margin=.25in,top=0.2cm,bottom=1.4cm,footskip=0.25in]{geometry}


% From https://tex.stackexchange.com/a/529773/152550
\newif\ifStart
\Startfalse
\newif\ifImage
\Imagefalse
\newif\ifCalloutRight
\CalloutRightfalse


\usepackage[skins,breakable]{tcolorbox}
\newsavebox\OurNiceSandBox
\tcbset{start/.code={\Starttrue},callout right/.code={\CalloutRighttrue},
shrink/.code={\tcbset{whatsapp cont/.style={hbox}}},
whatsapp cont/.style={width=\maxwidth},
whatsapp/.style={empty,breakable,
left=2mm,right=2mm,top=2mm,bottom=2mm,fontupper=\sffamily,
whatsapp cont,after skip=1ex},
whatsap skin/.style={% code for unbroken boxes:
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0) 
    -- ([yshift=-6mm]frame.north east); 
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0) 
    -- ([yshift=-6mm]frame.north west); 
 \fi
\fi},
interior code={
\path[fill=#1,rounded corners=3mm] 
 (frame.south west) rectangle (frame.north east);
},  
% code for the first part of a break sequence: 
skin first is subskin of={emptyfirst}{%
frame code={%
\ifStart
 \ifCalloutRight
    \path[fill=#1]([xshift=-3mm]frame.north east) -- ++ (9mm,0) 
    -- ([yshift=-6mm]frame.north east); 
 \else
    \path[fill=#1]([xshift=3mm]frame.north west) -- ++ (-9mm,0) 
    -- ([yshift=-6mm]frame.north west); 
 \fi
\fi},
interior code={
\path[fill=#1] 
 (frame.south west) [rounded corners=3mm] |-
 (frame.north)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},  
},
% code for the middle part of a break sequence: 
skin middle is subskin of={emptymiddle}{%
frame code={%
},
interior code={
\path[fill=#1] 
 (frame.south west) rectangle (frame.north east);
},  
},
% code for the last part of a break sequence: 
skin last is subskin of={emptylast}{%
frame code={%
},
interior code={
\path[fill=#1] 
 (frame.north west) [rounded corners=3mm] |-
 (frame.south)  -|  (frame.east) [rounded corners=0mm] |- cycle;
},  
}}}

\newtcolorbox{Mebox}[1][]{#1,whatsapp,flush left,whatsap skin=white}
\newcommand{\Me}[2][]{\begin{lrbox}{\OurNiceSandBox}
#2
\end{lrbox}%
\ifdim\wd\OurNiceSandBox<\maxwidth
\begin{Mebox}[shrink,#1]
#2
\end{Mebox}
\else
\begin{Mebox}[#1]
#2
\end{Mebox}
\fi}


\newtcolorbox{Youbox}[1][]{flush right,right skip=15mm,#1,whatsapp,callout right,
    whatsap skin=green!60!black}
\newcommand{\You}[2][]{\begin{lrbox}{\OurNiceSandBox}
#2
\end{lrbox}%
\ifdim\wd\OurNiceSandBox<\maxwidth
\begin{Youbox}[shrink,#1]
#2
\end{Youbox}
\else
\begin{Youbox}[#1]
#2
\end{Youbox}
\fi}

\newtcolorbox{Exercisebox}[1][]{#1,whatsapp,coltext=white,center,whatsap skin=blue}
\newcommand{\Exercise}[2][]{\begin{lrbox}{\OurNiceSandBox}
#2
\end{lrbox}%
\ifdim\wd\OurNiceSandBox<\maxwidth
\begin{Exercisebox}[shrink,#1]
#2
\end{Exercisebox}
\else
\begin{Exercisebox}[#1]
#2
\end{Exercisebox}
\fi}


\usepackage[explicit]{titlesec}

\usepackage{eso-pic}
\AddToShipoutPictureBG{\includegraphics[width=\paperwidth,height=\paperheight]{example-image}}

%%%%%%%%%%%%%%%%%%%%

\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{}

% From https://tex.stackexchange.com/a/529755/152550
\titleformat{\section}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \path[left color=red!40!black,right color=red!60!black,middle color=red] 
  (current page.north west) rectangle 
  ([yshift=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {Section \thesection: #1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
         \end{tikzpicture}
}[\vspace{1cm}]

% From https://tex.stackexchange.com/a/236778
\titleformat{name=\section,numberless}[display]{}{}{0pt}
  {\begin{tikzpicture}[overlay,remember picture]
  \path[left color=red!40!black,right color=red!60!black,middle color=red] 
  (current page.north west) rectangle 
  ([yshift=-2cm]current page.north east);
 \node[text=white,anchor=west,font=\LARGE\bfseries\sffamily,anchor=west]  (a)
    at ([xshift=3.5cm,yshift=-1cm]current page.north west)
   {#1};
   \clip ([xshift=2cm,yshift=-1cm]current page.north west)
    circle[radius=1.2cm];
   \node at 
   ([xshift=2cm,yshift=-1cm]current page.north west)
    {\includegraphics[width=1.8cm]{example-image}};
\end{tikzpicture}
}[\vspace{1cm}]

\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}

\Me[start]{\lipsum[1]\\ Text}

\Me{\includegraphics[scale=0.75]{example-image}}

\You[start]{\includegraphics{example-image-duck}}

\Exercise{How many marmots?}

\You[start]{\lipsum[2]}

\Me[start]{\lipsum[1-3]}

\Me{\lipsum[4]}

\You[start,spread sidewards=-3cm]{Quick and dirty.}

\Me[start,left skip=2cm]{MEOW!}

\newpage

\section{You are a duck}

\Me{\lipsum[1]}

\You{\lipsum[2]}


\newpage 
\section*{Bibliography}

\end{document}

enter image description here

  • Thank you!! The text is cropped correctly. You have done a nice refactoring!! I need some stuff of the previous answers, though. As you have mention, there is a problem with the image boxes; they have a lot of space on its right, because the images are also aligned to the left. I need the shapes to fit without spaces, including images (i.e. automatically). See the comparison: imgur.com/e5VtIgT.png I have defined \maxwidth{16cm} for the max width for any shape (it can be an image, normal text etc.). But I defined \maxheight{20cm} only for max height of shapes containing only images. – manooooh Feb 29 '20 at 03:24
  • This maximums values are only in case the text or image are too long; but adjustable shapes according to the text length (like your previous source codes, which are also nice) must be included on this new version. – manooooh Feb 29 '20 at 03:27
  • @manooooh Just to be clear: you want to have boxes that contain some graphics (say) and are just as wide as their content, right? –  Feb 29 '20 at 04:23
  • Yes, exactly. With a max size so that a box does not occupy a whole page. – manooooh Feb 29 '20 at 04:38
  • @manooooh OK, please have a look. –  Feb 29 '20 at 05:02
  • Thank you!! 1) I would prefer graphics not be an optional argument but a command that is mandatory for all shapes. That's not a real problem, though. 2) I would like not to have an environment like \begin{You}[...]...\end{You} but, as your previous answers, \You[...]{...} but this is not a problem. 3) In one of the interior code you wrote \path[fill=white] when it should be \path[fill=#1]! Hahaha. Thank you again!! – manooooh Feb 29 '20 at 20:50
  • 1
    @manooooh As for 2) : fixed and 3) good catch! fixed. As for 1), I do not understand this item. What should the usage look like? –  Mar 01 '20 at 00:54
  • As for 1): You wrote that graphics word is used when the box needs to be adjustable ("will adjust its size to the content"). I want all boxes to be adjustable, hence I will have to add graphics parameter to all \Me and \You of the document. I would like to not write graphics every time. That's all! – manooooh Mar 01 '20 at 00:57
  • @manooooh But the text boxes need to have a certain width at which the lines get broken. If you made them freely adjustable, the text would run out of the right margin. (If you want to break text only if it exceeds a maximal width, this is nontrivial and all solutions I know are a bit shaky.) –  Mar 01 '20 at 00:58
  • Oh, that's was my wish. If it is shaky then your answer is OK for me. But now I am having problems with \You and \Me widths. See image of your compiled code: https://imgur.com/eoylHne.png – manooooh Mar 01 '20 at 01:13
  • 1
    @manooooh My bad! Fixed it now. (I misunderstood what \newtcbox does. I will also add an experimental version which makes the width as you intend. –  Mar 01 '20 at 01:20
  • Your answer is now perfect. You have to do nothing else, but I am trying to shift boxes more to the center of the page, instead of sticking them to the left & right margins, respectively. For Mebox, I've tried to use {#1,whatsapp,xshift=2mm,flush left,whatsap skin=white} (see the xshift) to move Me box a little bit to the right, but I couldn't. I think it is due flush left. – manooooh Mar 01 '20 at 01:29
  • @manooooh I added a second version which is probably more versatile, i.e. it shrinks the boxes automatically. xshift is a pgf key. For tcolorbox you can use left skip, e.g. \Me[start,left skip=5cm]{MEOW!}. –  Mar 01 '20 at 01:34
  • left skip worked for the Me, but I am trying with You and there are no satisfactory results. Should I use left skip=<10 or 11 or ...>cm for You? – manooooh Mar 01 '20 at 01:40
  • @manooooh Naively I would have thought that right skip will do the trick, but no. I do not understand tcolorbox well enough to judge whether or not this is a bug. Experimentally spread sidewards seems to work. I added an example. –  Mar 01 '20 at 01:51
  • Yep, I'm having some problems with itemize and numbered lists, among code sample with listings. That's due the elimination of graphics (I think because of how Me and You are defined). As I said there is no problem adding each time graphics for images, and you said that solutions without graphics were shaky, so I was warned. I will use your first code. Thanks! – manooooh Mar 02 '20 at 00:31
  • @manooooh I think you can still use the second solution and whenever you put itemize or something of that sort in, you can just add width=\maxwidth (or define a style for that). Notice that for listings tcolorbox offers a wealth of options, some of which I learned when writing https://tex.stackexchange.com/a/517985/194703. It is definitely the right tool for that (as opposed to tikzpictures). –  Mar 02 '20 at 02:06
  • But tcolorbox implementation is part from the first code (of your actual answer), isn't it? – manooooh Mar 02 '20 at 02:37
  • @manooooh Both are. –  Mar 02 '20 at 02:38
  • Yes. Thus I don't understand why you say tcolorbox is good if both of your codes have it. What I am saying is that your first code is much better for me because I can use some stuff that I can't with the other. – manooooh Mar 02 '20 at 02:44
  • @manooooh My older answers had TikZ. Sorry, it was indeed confusing. –  Mar 02 '20 at 02:56
  • Using your second code, when I write \Me{Hello\\ bye} the output is Hellobye. Instead, when I write \Me{\lipsum[1]\\ bye} the bye is in a new line. I think this happens because Hello does not break into 2 or more lines i.e. boxes with one line of length does not break into a new line. Is this expected or is it a problem? P.S. I am creating a new question asking for the implemenation of lists & code samples using your second code. I've searched with no results. Thanks! – manooooh Mar 04 '20 at 20:40
  • 1
    @manooooh This is the difference between having a fixed width and not having a fixed width. Unfortunately, there is no really stable way to let LaTeX decide whether or not is should have a fixed width. You could do \Me{\begin{tabular}{@{}l@{}}Hello\\ bye\end{tabular}}, though. –  Mar 04 '20 at 22:51