Tikz 框图定位

Tikz 框图定位

我正在尝试将 tikz 框图放置在页面中央。我尝试使用“figure”环境,但仍然不起作用。这是我的代码:

\documentclass{article}
\usepackage[margin=1.0in]{geometry}

\usepackage{amsmath,amsfonts,amssymb,amsthm,mathrsfs}
\usepackage{float}
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{amsmath,bm,times}
\newcommand{\mx}[1]{\mathbf{\bm{#1}}} % Matrix command
\newcommand{\vc}[1]{\mathbf{\bm{#1}}} % Vector command

\begin{document}
\lipsum[23]

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\tikzstyle{OrderingStyle} = [draw, fill = red!20, text width = 9em, text centered, minimum height = 4em, rounded corners]
\tikzstyle{WindowStyle} = [draw, fill = blue!20, text width=9em, text centered, minimum height = 4em, rounded corners]
\tikzstyle{FunctionalStyle} = [draw, fill = green!20, text width = 9em, text centered, minimum height = 4em, rounded corners]
\tikzstyle{ann} = [above, text width=5em]
\def\blockdist{2.3}
\def\edgedist{0.8}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}
            
        \node (order) [OrderingStyle] {$O\colon \mathcal{LB}\rightarrow \mathcal{LB}$};
        
        \path (order)+(-2-\blockdist,0) node (window) [WindowStyle] {$W_{t,L}\colon \mathcal{LB}\rightarrow \mathcal{LB}$};
        
        \path (window)+(-3.8,0) node (input) [ann] {};
        \path [draw, ->] (input) -- node [above left] {$f$} 
        (window.west |- input) ;
        
        \path (order)+(2+\blockdist,0) node (functional) [FunctionalStyle] {$\psi\colon \mathcal{LB}\rightarrow R$};
        
        
        \path [draw, ->] (window) -- node [above] {$f_{w}$} 
        (order.west |- window) ;
        
        \path [draw, ->] (order) -- node [above] {$\hat{f}_{w}$} 
        (functional.west |- order) ;
        
        \node (nombreventana) [below of=window] {Window};
        \node (nombreorden) [below of=order] {Order};
        \node (nombrefunct) [below of=functional] {Functional};
        
        \path (order.south)+(0,-1.4) node (AMF) {Filter blocks};
        
        \draw [->] (functional.east) -- node [ann] {} + (\edgedist,0) 
        node[above right] {$\widetilde{f}$};
        
        \begin{pgfonlayer}{background}
            \path (window.west |- functional.north)+(-0.5,0.5) node (a) {};
            \path (AMF.south -| functional.east)+(+0.5,-0.2) node (b) {};
            \path[fill=yellow!20,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (window.north west)+(-0.2,0.2) node (a) {};
            \path (nombreventana.south -| window.east)+(+0.2,-0.2) node (b) {};
            \path[fill=blue!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (order.north west)+(-0.2,0.2) node (a) {};
            \path (nombreorden.south -| order.east)+(+0.2,-0.2) node (b) {};
            \path[fill=red!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (functional.north west)+(-0.2,0.2) node (a) {};
            \path (nombrefunct.south -| functional.east)+(+0.2,-0.2) node (b) {};
            \path[fill=blue!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
        \end{pgfonlayer}
    \end{tikzpicture}
    \caption{Block diagram}
\end{figure}
\lipsum[23]

\end{document}

结果如下: 在此处输入图片描述

答案1

它看起来不居中的原因是图表本身在左侧有很多空白。例如,您可以通过将环境放在tikzpicture\fbox{<tikzpicture here>}\draw (current bounding box.south west) rectangle (current bounding box.north east);在 之前添加 来形象化这一点\end{tikzpicture}。这样做会向您显示如下内容:

在此处输入图片描述

首先,您在那里有空白的原因就是节点input,因为您使用了ann样式,而样式又具有text width=5em。添加draw到节点的选项以查看以下内容:

在此处输入图片描述

为了解决这个问题,请修改节点的定义input,例如

\path (window)+(-2.8,0) node (input) {};

在下面的代码中,我还做了一些其他更改,代码中的注释已指出。

在此处输入图片描述

\documentclass{article}
\usepackage[margin=1.0in]{geometry}

\usepackage{amsmath,amsfonts,amssymb,amsthm,mathrsfs}
\usepackage{float}
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{
  shapes,
  arrows.meta, % not used, but the old arrows library is considered deprecated in favor of this
  positioning % for the <position>=of <other node> syntax
  }

% the times package is considered deprecated, currently the recommended
% package for times fonts is newtxtext
% add the newtxmath package if you want times math as well
\usepackage{bm,newtxtext}
\newcommand{\mx}[1]{\mathbf{\bm{#1}}} % Matrix command
\newcommand{\vc}[1]{\mathbf{\bm{#1}}} % Vector command


\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\tikzset{ % usually recommended over \tikzset
 OrderingStyle/.style={draw, fill = red!20, text width = 9em, text centered, minimum height = 4em, rounded corners},
WindowStyle/.style={draw, fill = blue!20, text width=9em, text centered, minimum height = 4em, rounded corners},
FunctionalStyle/.style={draw, fill = green!20, text width = 9em, text centered, minimum height = 4em, rounded corners},
ann/.style={above, text width=5em}
}
% one usually recommends \newcommand over \def to avoid accidentally overwriting existing macros
\newcommand\blockdist{2.3}
\newcommand\edgedist{0.8}

\begin{document}
\lipsum[23]

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[node distance=1mm] % modified the node distance because
            
        \node (order) [OrderingStyle] {$O\colon \mathcal{LB}\rightarrow \mathcal{LB}$};
        
        \path (order)+(-2-\blockdist,0) node (window) [WindowStyle] {$W_{t,L}\colon \mathcal{LB}\rightarrow \mathcal{LB}$};
        
        \path (window)+(-2.8,0) node (input) {};
        \path [draw, ->] (input) -- node [above left] {$f$} 
        (window.west |- input) ;
        
        \path (order)+(2+\blockdist,0) node (functional) [FunctionalStyle] {$\psi\colon \mathcal{LB}\rightarrow R$};
        
        
        \path [draw, ->] (window) -- node [above] {$f_{w}$} 
        (order.west |- window) ;
        
        \path [draw, ->] (order) -- node [above] {$\hat{f}_{w}$} 
        (functional.west |- order) ;
        
        % here I swapped below of= to below=of
        \node (nombreventana) [below=of window] {Window};
        \node (nombreorden) [below=of order] {Order};
        \node (nombrefunct) [below=of functional] {Functional};
        
        \path (order.south)+(0,-1.4) node (AMF) {Filter blocks};
        
        \draw [->] (functional.east) -- node [ann] {} + (\edgedist,0) 
        node[above right] {$\widetilde{f}$};
        
        \begin{pgfonlayer}{background}
            \path (window.west |- functional.north)+(-0.5,0.5) node (a) {};
            \path (AMF.south -| functional.east)+(+0.5,-0.2) node (b) {};
            \path[fill=yellow!20,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (window.north west)+(-0.2,0.2) node (a) {};
            \path (nombreventana.south -| window.east)+(+0.2,-0.2) node (b) {};
            \path[fill=blue!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (order.north west)+(-0.2,0.2) node (a) {};
            \path (nombreorden.south -| order.east)+(+0.2,-0.2) node (b) {};
            \path[fill=red!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
            \path (functional.north west)+(-0.2,0.2) node (a) {};
            \path (nombrefunct.south -| functional.east)+(+0.2,-0.2) node (b) {};
            \path[fill=blue!10,rounded corners, draw=black!50, dashed]
            (a) rectangle (b);
            
        \end{pgfonlayer}
    \end{tikzpicture}
    \caption{Block diagram}
\end{figure}
\lipsum[23]
\end{document}

相关内容