如何使用特定节点将 tikzpicture 在 beamer 框架中水平居中?

如何使用特定节点将 tikzpicture 在 beamer 框架中水平居中?

以下 MWE 将我的图像tikzpicture置于框架的中心:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\begin{frame}
  \frametitle{TEST}
  \begin{itemize}
  \item 1
  \item 2
  \item 3
  \item 4
  \item 5  This  is very  very  very  very  very  long  to illustrate the question
  \item 6
  \end{itemize}
  \centering
  \begin{tikzpicture}
    \node[draw=black, rectangle]             (a) {A};
    \node[draw=black, rectangle, below=of a] (b) {B};
    \node[draw=black, rectangle, below=of b] (c) {C};
    \draw[-latex] (a) -- (b);
    \draw[-latex] (b) -- (c);
    \draw[-latex] (c) -- +(25mm, 0);
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}

\end{document}

我怎样才能使这个图形居中,使得节点B位于框架的中心而不是整体的中心tikzpicture

答案1

密钥center coordinate定义为

\tikzset{
  center coordinate/.style={
    execute at end picture={
      \path ([rotate around={180:#1}]perpendicular cs: horizontal line through={#1},
                                  vertical line through={(current bounding box.east)})
            ([rotate around={180:#1}]perpendicular cs: horizontal line through={#1},
                                  vertical line through={(current bounding box.west)});}}}

并在 TikZ 图片的开头使用(工作原理类似于baselinetrim lefttrim right),如下所示:

\begin{tikzpicture}[center coordinate=(b)]

将创建一个 TikZ 图片,其中 — 在这种情况下 — (b)(或者对我来说更准确地说(b.center))将位于 TikZ 图片边界框的水平中心。

\centering与或环境一起使用center,这意味着 TikZ 图片将位于当前段落宽度 ( \linewidth) 的中心。然而,这将不是自动地中心当前页面上的 TikZ 图片(或者其中的坐标)。

当电流\linewidth在水平方向上不对称时,例如双面文档或双列文档中的一列,此方法将会失败。

在下面的示例中,我使用该backgrounds库及其framed键来显示扩展的边界框。

我还将 TikZ 图片和\centering宏放在\par一个组内,这样图片后面的文本也不会居中。您也可以使用center只有 TikZ 图片的环境,但这会引入额外的垂直空间。

代码

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,backgrounds}
\setbeamertemplate{frametitle}[default][center]
\tikzset{
  center coordinate/.style={
    execute at end picture={
      \path ([rotate around={180:#1}]perpendicular cs: horizontal line through={#1},
                                  vertical line through={(current bounding box.east)})
            ([rotate around={180:#1}]perpendicular cs: horizontal line through={#1},
                                  vertical line through={(current bounding box.west)});}}}
\begin{document}
\begin{frame}
  \frametitle{TEST}
  \begin{itemize}
  \item 1 \item 2 \item 3 \item 4
  \item 5  This  is very  very  very  very  very  long  to illustrate the question
  \item 6
  \end{itemize}
  {\centering
     \begin{tikzpicture}[center coordinate=(b), framed]
     \node[draw=black, rectangle]             (a) {A};
     \node[draw=black, rectangle, right=of a] (b) {B};
     \node[draw=black, rectangle, right=of b] (c) {C};
     \draw[-latex] (a) -- (b);
     \draw[-latex] (b) -- (c);
     \draw[-latex] (c) -- +(25mm, 0);
  \end{tikzpicture}
  \par}
  \begin{itemize}
  \item A \item B
  \end{itemize}
\end{frame}
\end{document}

答案2

在这种情况下,我认为trim lefttrim right键可以让你得到大部分你想要的东西。它们可以将坐标作为其值:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\setbeamertemplate{frametitle}[default][center]
\begin{document}
\begin{frame}{test}
\begin{itemize}
  \item 1
  \item 2  This  is very  very  very  very  very  long  to illustrate the question
  \item 3
  \end{itemize}
  \centering
  \begin{tikzpicture}[trim left=(b), trim right=(b)]
    \node[draw=black, rectangle]             (a) {A};
    \node[draw=black, rectangle, below=of a] (b) {B};
    \node[draw=black, rectangle, below=of b] (c) {C};
    \draw[-latex] (a) -- (b);
    \draw[-latex] (b) -- (c);
    \draw[-latex] (c) -- +(2, 0);
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}
\end{document}

在此处输入图片描述

答案3

您可以将节点的坐标设置为页面的中心。我希望这是你想要的。

% arara: pdflatex
% arara: pdflatex

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\setbeamertemplate{frametitle}[default][center]

\begin{document}    
    \begin{frame}
        \frametitle{TEST}
        \begin{tikzpicture}[remember picture,overlay] 
        \node[draw=black, rectangle] (b) at (current page.center) {B};
        \node[draw=black, rectangle, left=of b] (a) {A};
        \node[draw=black, rectangle, right=of b] (c) {C};
        \draw[-latex] (a) -- (b);
        \draw[-latex] (b) -- (c);
        \draw[-latex] (c) -- +(25mm, 0);
        \end{tikzpicture}
    \end{frame} 
\end{document}

答案4

也许是这样的?

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,tikzmark}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\begin{frame}
  \frametitle{TEST}
  \begin{itemize}
  \item 1
  \item 2
  \item 3
  \item 4
  \item 5  This  is very  very  very  very  very  long  to illustrate the question
  \item 6
  \end{itemize}
  \mbox{\tikzmark{b}}\par
  \begin{tikzpicture}[overlay,remember picture]
    \node[draw=black, rectangle] (b) at ({pic cs:b} -| current page.center) {B};
    \node[draw=black, rectangle, left=of b] (a) {A};
    \node[draw=black, rectangle, right=of b] (c) {C};
    \draw[-latex] (a) -- (b);
    \draw[-latex] (b) -- (c);
    \draw[-latex] (c) -- +(25mm, 0);
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}

\end{document}

以 b 为中心?

或者

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\begin{frame}
  \frametitle{TEST}
  \begin{itemize}
  \item 1
  \item 2
  \item 3
  \item 4
  \item 5  This  is very  very  very  very  very  long  to illustrate the question
  \item 6
  \end{itemize}
  \begin{tikzpicture}[overlay,remember picture, baseline=(x)]
    \coordinate (x);
    \node[draw=black, rectangle] (b) at (x -| current page.center) {B};
    \node[draw=black, rectangle, left=of b] (a) {A};
    \node[draw=black, rectangle, right=of b] (c) {C};
    \draw[-latex] (a) -- (b);
    \draw[-latex] (b) -- (c);
    \draw[-latex] (c) -- +(25mm, 0);
  \end{tikzpicture}
  \begin{itemize}
  \item A
  \item B
  \end{itemize}
\end{frame}

\end{document}

以 b 为中心,没有 tikzmark?

相关内容