LaTeX Tikz 图表未在页面居中

LaTeX Tikz 图表未在页面居中

我是 LaTex 的新手,所以如果这太基础了,请原谅我。我使用 Tikz 创建了一个图形,但不确定如何让它在页面上居中。我试过了,\center\begin{centered}这些都不能解决问题。正如另一个问题所建议的那样,我尝试将它做成一个图形,但如果有什么的话,那就是它让情况变得更糟了,把它推到了文本上方!

我用它\usepackage{showframe}来演示这个问题,并显示图形比页面宽。对于像我这样的新手来说,任何建议都很重要 :)

文档截图:

在此处输入图片描述

用于创建它的 LaTex:

% ADAPTED FROM:
% Author: Robert Felty
% Source: http://blog.robfelty.com/2007/02/14/pgf-gallery
% Model structure from Rumelhart \& McClelland (1986, p .222)%

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{showframe} % To demonstrate issue

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\tikzstyle{information text}=[text badly centered,font=\small,text width=2.6cm]

\section{Simple Neural Network}

\begin{figure}
\begin{tikzpicture}[scale=.8]
    \foreach \xa / \xb in {1 / 5, 5 / 9}
        \foreach \ya / \yb / \yc in {2 / 3 / 4, 3 / 4 / 2, 4 / 2 / 3}
        {
            \draw (\xa,\ya) -- (\xb,\ya);
            \draw (\xa,\ya) -- (\xb,\yb);
            \draw (\xa,\ya) -- (\xb,\yc);
        }

    \node at (1,1.3)[information text]{...};
    \node at (5,1.3)[information text]{...};
    \node at (9,1.3)[information text]{...};

    \draw[->] (1,0.4) node[below, information text]
        {Input Neurons (Pixels of Image) $\times$ 784} -- (1,1);
    \draw[->] (5,0.4) node[below,information text]
        {Hidden Neurons $\times$ 100} -- (5,1);
    \draw[->] (9,0.4) node[below,information text]
        {Output Neurons (Confidence of each digit) $\times$ 10} -- (9,1); 

    \draw (3,5) node[information text] { Input $\rightarrow$ Hidden Weights };
    \draw (7,5) node[information text] { Hidden $\rightarrow$ Output Weights };

    \foreach \y in {2,3,4} {
        \draw[->] (-0.5,\y) node[left, information text]{} -- (0.7,\y);
        \draw[->] (9.3,\y) node[left, information text]{} -- (10.5,\y);
        \foreach \x in {1,5,9}
        \filldraw[fill=white] (\x,\y) circle (0.3);
    }
\end{tikzpicture}
\caption{The structure of the network.}
\end{figure}

\end{document}

答案1

问题只是您在图表左侧添加了一些空节点,这些节点在输出中不可见,但确实会影响边界框。如果您添加draw到样式中information text,您将获得以下输出:

在此处输入图片描述

注意左侧的三个空框。它们是在循环中生成的\foreach,因此要删除它们,请将循环中的前两行修改为

\draw[->] (-0.5,\y)  -- (0.7,\y);
\draw[->] (9.3,\y) -- (10.5,\y);

即删除node[left,information text] {}

之后,\centering在 后添加\begin{figure},得到如下结果:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{showframe} % To demonstrate issue

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\tikzset{information text/.style={text badly centered,font=\small,text width=2.6cm}}

\section{Simple Neural Network}

\begin{figure}
\centering
\begin{tikzpicture}[scale=.8]
    \foreach \xa / \xb in {1 / 5, 5 / 9}
        \foreach \ya / \yb / \yc in {2 / 3 / 4, 3 / 4 / 2, 4 / 2 / 3}
        {
            \draw (\xa,\ya) -- (\xb,\ya);
            \draw (\xa,\ya) -- (\xb,\yb);
            \draw (\xa,\ya) -- (\xb,\yc);
        }

    \node at (1,1.3)[information text]{...};
    \node at (5,1.3)[information text]{...};
    \node at (9,1.3)[information text]{...};

    \draw[->] (1,0.4) node[below, information text]
        {Input Neurons (Pixels of Image) $\times$ 784} -- (1,1);
    \draw[->] (5,0.4) node[below,information text]
        {Hidden Neurons $\times$ 100} -- (5,1);
    \draw[->] (9,0.4) node[below,information text]
        {Output Neurons (Confidence of each digit) $\times$ 10} -- (9,1); 

    \draw (3,5) node[information text] { Input $\rightarrow$ Hidden Weights };
    \draw (7,5) node[information text] { Hidden $\rightarrow$ Output Weights };

    \foreach \y in {2,3,4} {
        \draw[->] (-0.5,\y)  -- (0.7,\y);
        \draw[->] (9.3,\y) -- (10.5,\y);
        \foreach \x in {1,5,9}
        \filldraw[fill=white] (\x,\y) circle (0.3);
    }
\end{tikzpicture}
\caption{The structure of the network.}
\end{figure}

\end{document}

答案2

显然,您的图片并不对称于其自身的中心。我曾经\framebox想象过 的边界tikzpicture

在此处输入图片描述

% ADAPTED FROM:
% Author: Robert Felty
% Source: http://blog.robfelty.com/2007/02/14/pgf-gallery
% Model structure from Rumelhart \& McClelland (1986, p .222)%

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{showframe} % To demonstrate issue

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\tikzstyle{information text}=[text badly centered,font=\small,text width=2.6cm]

\section{Simple Neural Network}

\begin{figure}
\centering % <------------------- Added
\framebox{ % <------------------- Added
\begin{tikzpicture}[scale=.3]
    \foreach \xa / \xb in {1 / 5, 5 / 9}
        \foreach \ya / \yb / \yc in {2 / 3 / 4, 3 / 4 / 2, 4 / 2 / 3}
        {
            \draw (\xa,\ya) -- (\xb,\ya);
            \draw (\xa,\ya) -- (\xb,\yb);
            \draw (\xa,\ya) -- (\xb,\yc);
        }

    \node at (1,1.3)[information text]{...};
    \node at (5,1.3)[information text]{...};
    \node at (9,1.3)[information text]{...};

    \draw[->] (1,0.4) node[below, information text]
        {Input Neurons (Pixels of Image) $\times$ 784} -- (1,1);
    \draw[->] (5,0.4) node[below,information text]
        {Hidden Neurons $\times$ 100} -- (5,1);
    \draw[->] (9,0.4) node[below,information text]
        {Output Neurons (Confidence of each digit) $\times$ 10} -- (9,1); 

    \draw (3,5) node[information text] { Input $\rightarrow$ Hidden Weights };
    \draw (7,5) node[information text] { Hidden $\rightarrow$ Output Weights };

    \foreach \y in {2,3,4} {
        \draw[->] (-0.5,\y) node[left, information text]{} -- (0.7,\y);
        \draw[->] (9.3,\y) node[left, information text]{} -- (10.5,\y);
        \foreach \x in {1,5,9}
        \filldraw[fill=white] (\x,\y) circle (0.3);
    }
\end{tikzpicture}
} % <------------------- Added
\caption{The structure of the network.}
\end{figure}

\end{document}

编辑

按照建议Torbjørn T.(添加draw)您会看到占据空间的额外空框。

\tikzstyle{information text}=[text badly centered,font=\small,text width=2.6cm,draw]

在此处输入图片描述

相关内容