这是前两个问题的组合(及其各自的答案):如何为页面顶部旁边带有 svg 的部分创建命令?和如何使用tikzpicture
环境与矩形形状进行对话?
问题
请考虑这个 MWE(它很长因为我需要显示实际的页眉和页脚):
\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}
此组合源代码来自@薛定谔的猫一旦所提问题的答案被接受,他们就会做出修改。
查看输出:
缺少标题。有时它会与形状发生冲突。
此外,如果我们增加一个图像尺寸,比如说我们改为\You[start,image={scale=1}]{example-image-duck}
,\You[start,image={scale=1.35}]{example-image-duck}
就会与页脚发生冲突:
问题
有没有办法防止与页眉和页脚发生冲突,同时保持大小?我不在乎一个不适合页面的形状是否必须转到下一页。我不想发生冲突。
答案1
此代码翻译了 Ti钾基于 Z 的形状变成tcolorbox
es。这样做的好处是它们可破坏。因此您浪费的空间更少。如果您使用键graphics
,框将根据内容调整其大小。
\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}
这是一个版本,如果框小于,它们就会缩小,\maxwidth
即您不再需要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}