我正在进行对话,我需要绘制一些形状,并将它们正确放置在页面上。这些形状如下:
- 带有小徽章的白色矩形(称为
mestart
): - 没有徽章的白色矩形(称为
me
): - 带有小徽章的绿色矩形(称为
youstart
): - 没有徽章的绿色矩形(称为
you
): - 一个带圆角的蓝色矩形(称为
exnumber
):
我想要的是
我想要引发以下对话:
需要考虑:
- 所有矩形(除
exnumber
)内部都可以有:- 仅限文本(普通文本、项目符号列表、编号列表等),或
- 仅一张图片(可以是
tikzpicture
),或者 - 文本和示例代码(例如
listings
包可以作为一个很好的例子)。
- 这些矩形必须具有最大宽度并且它们之间必须有间距: (图像也应该具有相同的最大宽度)。
- 蓝色矩形(即,
exnumber
应放置在页面中央,并自动编号,并在章节结束时重新开始。例如:在第 1 节中Exercise 1
,,Exercise 2
。Exercise 3
在第 2 节中:Exercise 1
,Exercise 2
。
我做了什么
请考虑这个MWE:
\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{shapes.symbols}
\begin{document}
\begin{tikzpicture}
\node (1) [rounded corners=.2cm,inner sep=.3cm,align=right,fill=blue] {Exercise 1};
\end{tikzpicture}
\begin{tikzpicture}
\node (1) [draw,rounded corners=.2cm,inner sep=.5cm,align=left] {rounded rectangle\\asas};
\end{tikzpicture}
\begin{tikzpicture}
\node (1) [rounded corners=.2cm,inner sep=.5cm,align=right,fill=green] {This is a rounded rectangle\\Text\\ a \\ a \\ a};
\end{tikzpicture}
\end{document}
编辑一些改变编辑令人惊叹的薛定谔猫的答案#4有一些实现:
- 已添加
\maxwidth
和\maxheight
。 - 由于我需要在对话中添加大量图像,因此我创建了四种新形状:四种形状,但
Image
名称后面带有单词。其中包括\includegraphics[max height=\maxheight,max width=\maxwidth]{#2}
。 \begin{varwidth}{\maxwidth}
添加到正常的 4 种形状中。- 在代码中添加了标签空间和一些信息。
最后结果:
\documentclass{article}
\usepackage{blindtext}
\def\maxwidth{16cm}
\def\maxheight{20cm}
\usepackage[a4paper,margin=.25in,footskip=0.25in]{geometry}
\usepackage[export]{adjustbox} % Loads also graphicx
\usepackage{varwidth}
\usepackage{tikz}
\usetikzlibrary{positioning}
% From https://tex.stackexchange.com/revisions/529773/4
\tikzset{basic/.style={rounded corners=.2cm,inner sep=.5cm},
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=.3cm,align=right,
fill=blue,anchor=north,font=\sffamily,text=white},
whatsapp/.cd,vdist/.initial=3mm,hdist/.initial=6mm
}
% Added varwidth to set a max width for each of the shapes. From https://tex.stackexchange.com/a/46479/152550
% MeStart
\newcommand{\MeStart}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[my callout L,alias=tmp,#1] {\begin{varwidth}{\maxwidth}#2\end{varwidth}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% Me
\newcommand{\Me}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[basic L,alias=tmp,#1] {\begin{varwidth}{\maxwidth}#2\end{varwidth}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% YouStart
\newcommand{\YouStart}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[my callout R,alias=tmp,#1] {\begin{varwidth}{\maxwidth}#2\end{varwidth}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% You
\newcommand{\You}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[basic R,alias=tmp,#1] {\begin{varwidth}{\maxwidth}#2\end{varwidth}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% Exercise
\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}
% max height/width for includegraphics from https://tex.stackexchange.com/a/47252/152550
% MeStartImage
\newcommand{\MeStartImage}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[my callout L,alias=tmp,#1] {\includegraphics[max height=\maxheight,max width=\maxwidth]{#2}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% MeImage
\newcommand{\MeImage}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[basic L,alias=tmp,#1] {\includegraphics[max height=\maxheight,max width=\maxwidth]{#2}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% YouStartImage
\newcommand{\YouStartImage}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[my callout R,alias=tmp,#1] {\includegraphics[max height=\maxheight,max width=\maxwidth]{#2}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
% YouImage
\newcommand{\YouImage}[2][]{\noindent\begin{tikzpicture}
\path (0,0) (\textwidth-\pgfkeysvalueof{/tikz/whatsapp/hdist},0) node[basic R,alias=tmp,#1] {\includegraphics[max height=\maxheight,max width=\maxwidth]{#2}};
\path (tmp.south) ++ (0,-\pgfkeysvalueof{/tikz/whatsapp/vdist});
\end{tikzpicture}\par}
\begin{document}
\begin{tikzpicture}[overlay,remember picture]
\fill[blue!20] (current page.south west) rectangle (current page.north east);
\end{tikzpicture}
\MeStart{\blindtext[1]\\ Text}
\MeImage[scale=0.5]{example-image}
\YouStartImage[scale=2]{example-image-duck}
\Exercise{How many marmots?}
\You{\Blindtext[1]}
\end{document}
答案1
这定义了这样的形状。请注意,我并没有真正简化定位,因为您可能需要一些用于对话的宏。他们可以处理这个问题。以下代码基于您问题中新添加的例程。但是,它没有定义许多宏,而是使用 pgf 键。例如,而不是
\MeStart{\blindtext[1]\\ Text}
您可以使用
\Me[start]{\blindtext[1]\\ Text}
或者
\You[start,image={scale=2}]{example-image-duck}
等等。这样做的好处是可以非常轻松地进行扩展,同时保持向后兼容性。
\documentclass{article}
\usepackage{blindtext}
\def\maxwidth{16cm}
\def\maxheight{20cm}
\usepackage[a4paper,margin=.25in,footskip=0.25in]{geometry}
\usepackage[export]{adjustbox} % Loads also graphicx
\usepackage{varwidth}
\newif\ifStart
\Startfalse
\newif\ifImage
\Imagefalse
\usepackage{tikz}
\usetikzlibrary{positioning}
% From https://tex.stackexchange.com/revisions/529773/4
\tikzset{basic/.style={rounded corners=.2cm,inner sep=.5cm},
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=.3cm,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}
\begin{document}
\begin{tikzpicture}[overlay,remember picture]
\fill[blue!20] (current page.south west) rectangle (current page.north east);
\end{tikzpicture}
\Me[start]{\blindtext[1]\\ Text}
\Me[image={scale=0.25}]{example-image}
\You[start,image={scale=2}]{example-image-duck}
\Exercise{How many marmots?}
\You{\Blindtext[1]}
\end{document}