![使用 Beamer 将 tikz 图片放置在 itemize 环境中项目的右侧](https://linux22.com/image/312324/%E4%BD%BF%E7%94%A8%20Beamer%20%E5%B0%86%20tikz%20%E5%9B%BE%E7%89%87%E6%94%BE%E7%BD%AE%E5%9C%A8%20itemize%20%E7%8E%AF%E5%A2%83%E4%B8%AD%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%8F%B3%E4%BE%A7.png)
我正在尝试将 tikz 图片放置在投影仪框架的右侧\item
,到目前为止,我能想到的最接近的结果是:
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\frame{
\frametitle{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \begin{minipage}[t]{1\linewidth}\raggedright
\begin{wrapfigure}{r}{0.5\textwidth}
\resizebox{!}{0.25\textheight}{
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\end{tikzpicture}
}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.
\end{minipage}
\end{itemize}
\end{block}
}
\end{document}
但是,我们可以看出对齐方式完全错误。文本从项目标记下方开始,并且 tikz 图形的垂直对齐方式非常奇怪。如何才能让其看起来更“正确”?
编辑:我已经更新了示例,以清楚地表明我希望第一个项目跨越整个宽度,第二个项目放置在图的左侧。
答案1
可以使用 使Atikzpicture
相对于基线对齐baseline
。也可以使用 缩放scale
。后者的一个优点是字体不会缩放,因此不会出现不同图形或图形与文本之间字体大小不匹配的情况。(当然,这不是总是一个优势!)
我会将文本放在一个简单的位置minipage
,然后tikzpicture
使用其原生键对齐和缩放。例如:
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\begin{frame}{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \begin{minipage}[t]{.5\linewidth}\raggedright
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.
\end{minipage}%
\begin{tikzpicture}[scale=.5, baseline=(c)]
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\path [fill=red] (current bounding box.north) ++(0,-\baselineskip) coordinate (c);
\end{tikzpicture}
\end{itemize}
\end{block}
\end{frame}
\end{document}
baseline
可以采用维度(例如1ex
)或坐标(例如c
或current bounding box.north
或其他)。有关详细信息,请参阅 TikZ 手册。(v3 手册中的第 125 页。)
答案2
这是一个没有使用 wrapfigure 和 resizebox 的解决方案。我还将文本放在了它自己的小页面中。只要您不想在图形周围环绕大量文本,这可能是一个可以接受的答案。
因为您想要图形左侧的文本,所以我将其放在第一位。
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\frame{
\frametitle{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{itemize}
\item Lorem
\item {\begin{minipage}[t]{.45\textwidth}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.\end{minipage}\begin{minipage}[h]{.45\textwidth}
\begin{flushright}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
width=\textwidth,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\end{tikzpicture}
\end{flushright}
\end{minipage}
}
\end{itemize}
\end{block}
}
\end{document}
结果:
编辑:第二个建议。
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\frame{
\frametitle{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{minipage}[b]{.49\textwidth}\begin{itemize}
\item Lorem
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.
\end{itemize}\end{minipage}\begin{minipage}[b]{.45\textwidth}
\begin{flushright}
\begin{tikzpicture}[baseline]
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
width=\textwidth,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\end{tikzpicture}
\end{flushright}
\end{minipage}
\end{block}
}
\end{document}
答案3
下面的代码展示了一个示例adjustbox
。
第二项行分为两个片段,第一个是模仿的调整框minipage
,第二个是另一个在顶部调整的框(valign=t
)与前一个框。
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\frame{
\frametitle{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \adjustbox{minipage=[t]{.45\textwidth}}{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.
}
\hfill
\adjustbox{valign=t, max width=.45\textwidth}
{
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\end{tikzpicture}}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\end{itemize}
\end{block}
}
\end{document}
答案4
这里我省去了wrapfigure
。相反,我将第二个项目文本(并且只有文本)放入minipage
宽度为 的顶部对齐 中.45\linewidth
。然后,我使用包\belowbaseline[-\ht\strutbox]{...}
的构造stackengine
将tikz
图形放入其中。这将使其与前一个 的顶部正确对齐minipage
。我还在\hfil
项目之间添加了 以提供一些水平分隔。
\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots,stackengine}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
\frame{
\frametitle{Lorem ipsum}
\begin{block}{Lorem ipsum}
\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item \begin{minipage}[t]{.45\linewidth}\raggedright
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
id lorem a placerat. Integer nec tincidunt risus.
\end{minipage}\hfil
\belowbaseline[-\ht\strutbox]{\resizebox{!}{0.25\textheight}{
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-2:3,samples=50,smooth},
axis x line*=bottom,
axis y line*=left,
enlargelimits=upper]
\addplot[color=red!75!black] {gauss(1,0.5)};
\addplot[color=red!75!black] {gauss(1,0.75)};
\addplot[color=red!75!black] {gauss(1,1)};
\addplot[color=red!75!black] {gauss(1,2)};
\end{axis}
\end{tikzpicture}
}}
\end{itemize}
\end{block}
}
\end{document}