并排放置 2 个不同的 tikz 图

并排放置 2 个不同的 tikz 图

我有两个不同的 tikz 图作为 .tex 文件,如何将它们并排放置?这些是由 @hpekristiansen 制作的(如何在 tikz 上绘制自定义函数?

图 1:(右)

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\begin{tikzpicture}[
state/.style={draw, circle, minimum size=0.8cm, inner sep=0pt},
arrow/.style={-Stealth, shorten >=1pt},
dot/.style={minimum size=4pt, inner sep=0pt, rounded corners=1pt, fill},
node distance=0.1cm and 2cm,
]
\node[state] (y1) {$y_1$};
\node[state, below=of y1] (y2) {$y_2$};
\node[state, below=0.8cm of y2] (yn) {$y_n$};
\node[state, below=of yn] (b) {$b$};
\node[state, above right=of y1] (a11) {$a_{11}$};
\node[state, below=of a11] (a21) {$a_{21}$};
\node[state, below right=of yn] (an1) {$a_{n1}$};
\node[state, below=of an1] (b1) {$b_1$};
\node[state, right=of a11] (ak1) {$a_{k1}$};
\node[state, right=of a21] (ak2) {$a_{k2}$};
\node[state, right=of an1] (ann) {$a_{nn}$};
\node[state, right=of b1] (bn) {$b_n$};
\node[state] at ($(ak2)!0.5!(ann)+(2,0)$) (sigma) {$\Sigma$};
\node[dot] at ($(a21)!0.3!(an1)$) {};
\node[dot] at ($(a21)!0.5!(an1)$) {};
\node[dot] at ($(a21)!0.7!(an1)$) {};
\node[dot] at ($(ak2)!0.3!(ann)$) {};
\node[dot] at ($(ak2)!0.5!(ann)$) {};
\node[dot] at ($(ak2)!0.7!(ann)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.3!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.5!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.7!($(an1)!0.5!(ann)$)$) {};
\node[dot] (dot1) at ($(a11)!0.5!(ak1)$) {};
\node[dot] (dot2) at ($(b1)!0.5!(bn)$) {};
\draw[arrow] (a11) -- (dot1) edge (ak1);
\draw[arrow] (b1) -- (dot2) edge (bn);
\draw[arrow] (y1) --node[above, font=\small]{$w_{11}^1$} (a11);
\draw[arrow] (y1) -- (a21);
\draw[arrow] (y2) --node[above, font=\small]{$w_{11}^1$} (a21);
\draw[arrow] (y2) -- (a21);
\draw[arrow] (y2) -- (an1);
\draw[arrow] (yn) -- (a11);
\draw[arrow] (yn) --node[above, font=\small]{$w_{11}^1$} (an1);
\draw[arrow] (yn) --node[above, font=\small]{$w_{n1}^1$} (b1);
\draw[arrow] (b) -- (a11);
\draw[arrow] (b) -- (an1);
\draw[arrow] (b) -- (b1);
\draw[arrow] (ak1) -- (sigma);
\draw[arrow] (ak2) -- (sigma);
\draw[arrow] (ann) -- (sigma);
\draw[arrow] (bn) -- (sigma);
\draw[arrow] (sigma) -- +(1,0)node[right]{$I$};
\end{tikzpicture}
\end{document}

图 2:(左)

\documentclass[tikz, border=1cm]{standalone}
\tikzset{>=latex}
\begin{document}
\begin{tikzpicture}[
declare function={f(\x)=sin((\x-2)^2 r)+2;},
]
\draw[blue!50!black] plot[domain={0.5:7}, samples=50, smooth] (\x,{f(\x)});
\draw[blue!50!black, densely dotted] (0,2) -- (7,2);
\draw[densely dashed] (2.0,0) node[below]{$x_1$} -- +(0,{f(2.0)}) node[above=4pt]{$y_1$};
\draw[densely dashed] (2.5,0) node[below]{$x_2$} -- +(0,{f(2.5)}) node[above=4pt]{$y_2$};
\draw[densely dashed] (3.0,0) node[below]{$x_3$} -- +(0,{f(3.0)}) node[above=4pt]{$y_3$};
\draw[densely dashed] (3.5,0) node[below]{$x_4$} -- +(0,{f(3.5)}) node[above=4pt]{$y_4$};
\draw[densely dashed] (5.0,0) node[below]{$x_n$} -- +(0,{f(5.0)}) node[above right]{$y_n$};
\draw[thick, <->] (0,6) --node[above, rotate=90, font=\bfseries] (Y) {$f(x)$} (0,0) -- (7,0) node[below, font=\bfseries] {Quad. points};
\end{tikzpicture}
\end{document} 

答案1

嗯,怎么样<tikzpicture left> \qquad <tikzpicture right>

完成 MWE (Minimal Working Wxample),我利用 随意修改了图片代码chainsscopequotes使其稍微短一些。由于左图部分很宽,我通过定义x=8mm和在两行中写入横坐标名称来减小其宽度。

编辑:

  • 您没有提供有关文档布局的任何信息,因此在下面的 MWE 中使用article文档类的默认设置
  • 看看,到目前为止还没有提到你想要的数字的问题,现在添加:
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

表示页面布局(的上部)。

  • 不管怎样,左边的图片缩小了大约 20%,但两张图片仍然超出了页面的文本区域。下图中它们之间仍然有一段距离,用于\qquad确定它们之间的距离。
  • 正如你在测试中注意到的,编译这个 MWE 时会抛出警告
Overfull \hbox (102.33492pt too wide) in paragraph at lines 16--89

梅威瑟:

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\usepackage{tikz}
\usetikzlibrary{arrows.meta, 
                calc, chains,
                positioning, 
                quotes}  % added

\begin{document}

\noindent
\begin{tikzpicture}[x=0.8cm,
declare function={f(\x)=sin((\x-2)^2 r)+2;},
                    ]
\draw[blue!50!black] plot[domain={0.5:7}, samples=50, smooth] (\x,{f(\x)});
\draw[blue!50!black, densely dotted] (0,2) -- (7,2);
\draw[densely dashed] (2.0,0) node[below]{$x_1$} -- ++ (0,{f(2.0)}) node[above=4pt]{$y_1$};
\draw[densely dashed] (2.5,0) node[below]{$x_2$} -- ++(0,{f(2.5)}) node[above=4pt]{$y_2$};
\draw[densely dashed] (3.0,0) node[below]{$x_3$} -- ++(0,{f(3.0)}) node[above=4pt]{$y_3$};
\draw[densely dashed] (3.5,0) node[below]{$x_4$} -- ++(0,{f(3.5)}) node[above=4pt]{$y_4$};
\draw[densely dashed] (5.0,0) node[below]{$x_n$} -- ++(0,{f(5.0)}) node[above right]{$y_n$};
\draw[thick, <->] (0,4) -- node[above, rotate=90, font=\bfseries] (Y) {$f(x)$} (0,0) 
                        -- (7,0) node[below left, align=center, font=\bfseries] {Quad.\\ points};
    \end{tikzpicture}
\qquad% distance between tikzpictures
    \begin{tikzpicture}[
node distance = 3mm and 17mm,
  start chain = going below,
 state/.style = {circle, draw, minimum size=8mm, inner sep=0pt},
   arr/.style = {-Stealth, shorten >=1pt},
   dot/.style = {rounded corners=1pt, inner sep=2pt, fill},
       every edge/.append style = {arr},    % added
every edge quotes/.append style = {font=\footnotesize, pos=0.4, sloped}, % added
                        ]
    \begin{scope}[nodes={state, on chain}] % added
\node   (y1) {$y_1$};
\node   (y2) {$y_2$};
\node[below=8mm of y2] (yn) {$y_n$};
\node   (b) {$b$};
    \end{scope}
    \begin{scope}[nodes={state}]           % added
\node[above right=of y1] (a11) {$a_{11}$};
\node[above right=of y2] (a21) {$a_{21}$};
\node[below right=of yn] (an1) {$a_{n1}$};
\node[below right=of b]  (b1)  {$b_1$};

\node[right=of a11] (ak1) {$a_{k1}$};
\node[right=of a21] (ak2) {$a_{k2}$};
\node[right=of an1] (ann) {$a_{nn}$};
\node[right=of b1]  (bn)  {$b_n$};
\node[right=of {$(ak2)!0.5!(ann)$}] (sigma) {$\Sigma$};
    \end{scope}
    
\path   ($(a21)!0.3!(an1)$) node (d11) [dot] {}  
        ($(a21)!0.5!(an1)$) node (d12) [dot] {} 
        ($(a21)!0.7!(an1)$) node (d13) [dot] {}; % changed
\path   ($(ak2)!0.3!(ann)$) node (d31) [dot] {}
        ($(ak2)!0.5!(ann)$) node (d32) [dot] {}
        ($(ak2)!0.7!(ann)$) node (d33) [dot] {}; % changed
\path   ($(d11)!0.5!(d31)$) node (d31) [dot] {}
        ($(d12)!0.5!(d32)$) node (d32) [dot] {}
        ($(d13)!0.5!(d33)$) node (d33) [dot] {}; % changed
\path   ($(a11)!0.5!(ak1)$) node (d20) [dot] {} 
        ($(b1)!0.5!(bn)$)   node (d24) [dot] {}; % changed
        
\path   (a11) edge (d20) edge (ak1)
        (b1)  edge (d24) edge (bn);              % changed
        
\draw   (y1) edge ["$w_{11}^1$"] (a11)           % changed, used `quotes` library
        (y1) edge (a21)
        (y2) edge ["$w_{21}^1$"] (a21) 
        (y2) edge (an1)
        
        (yn) edge (a11) 
        (yn) edge ["$w_{11}^1$"] (an1)
        (yn) edge ["$w_{n1}^1$"] (b1);
        
\foreach \i in {a11, an1, b1}
    \draw[arr] (b) -- (\i);       % changed, used loops
\foreach \i in {ak1, ak2, ann, bn}
    \draw[arr] (\i) -- (sigma);   % changed, used loops
\draw[arr] (sigma) -- +(1,0) node[right]{$I$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

(红线表示页面布局)

附录:
以下是您的评论:

  • 你喜欢图片之间的距离更大
  • 从上图可知,只有在两种情况下才有可能出现上述没有警告的情况:
    • 当你使用包更改(目前未知)页面布局时,如下面的 MWE 中所做的那样geometry,或者
    • \textwidth仅通过使用例如包来对图形进行局部更改changepage
\documentclass{article}
% second case
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage[strict]{changepage}  % added
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, chains,
                positioning,
                quotes}  % added

\begin{document}
\begin{adjustwidth*}{}{-\dimexpr\marginparsep+\marginparwidth} % defined in changepage package
% left image
\hfill  % <--- make maximal possible distance between pictures
% right image
\end{adjustwidth}
\end{document}

经过两次编译后得到:

在此处输入图片描述

  • 关于所需的tikzpicture位置交换,当您更改页面布局时(例如,页面边框宽度为 25 毫米),请参阅下一个 MWE:

\documentclass{article}
\usepackage[margin=25mm]{geometry}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}% For dummy text. Don't use in a real document

\usepackage[strict]{changepage} % < added
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, chains,
                positioning,
                quotes}  % added

\begin{document}
\begin{figure}[ht]
\begin{tikzpicture}[x=0.8cm,
declare function={f(\x)=sin((\x-2)^2 r)+2;},
                    ]
\draw[blue!50!black] plot[domain={0.5:7}, samples=50, smooth] (\x,{f(\x)});
\draw[blue!50!black, densely dotted] (0,2) -- (7,2);
\draw[densely dashed] (2.0,0) node[below]{$x_1$} -- ++ (0,{f(2.0)}) node[above=4pt]{$y_1$};
\draw[densely dashed] (2.5,0) node[below]{$x_2$} -- ++(0,{f(2.5)}) node[above=4pt]{$y_2$};
\draw[densely dashed] (3.0,0) node[below]{$x_3$} -- ++(0,{f(3.0)}) node[above=4pt]{$y_3$};
\draw[densely dashed] (3.5,0) node[below]{$x_4$} -- ++(0,{f(3.5)}) node[above=4pt]{$y_4$};
\draw[densely dashed] (5.0,0) node[below]{$x_n$} -- ++(0,{f(5.0)}) node[above right]{$y_n$};
\draw[thick, <->] (0,4) -- node[above, rotate=90, font=\bfseries] (Y) {$f(x)$} (0,0)
                        -- (7,0) node[below left, align=center, font=\bfseries] {Quad.\\ points};
\end{tikzpicture}%
\hfill  % <--- make maximal possible distance between pictures
\begin{tikzpicture}[
node distance = 3mm and 15mm,
  start chain = going below,
 state/.style = {circle, draw, minimum size=8mm, inner sep=0pt},
   arr/.style = {-Stealth, shorten >=1pt},
   dot/.style = {rounded corners=1pt, inner sep=2pt, fill},
       every edge/.append style = {arr},    % added
every edge quotes/.append style = {font=\footnotesize, pos=0.4, sloped}, % added
                        ]
    \begin{scope}[nodes={state, on chain}] % added
\node   (y1) {$y_1$};
\node   (y2) {$y_2$};
\node[below=8mm of y2] (yn) {$y_n$};
\node   (b) {$b$};
    \end{scope}
    \begin{scope}[nodes={state}]           % added
\node[above right=of y1] (a11) {$a_{11}$};
\node[above right=of y2] (a21) {$a_{21}$};
\node[below right=of yn] (an1) {$a_{n1}$};
\node[below right=of b]  (b1)  {$b_1$};

\node[right=of a11] (ak1) {$a_{k1}$};
\node[right=of a21] (ak2) {$a_{k2}$};
\node[right=of an1] (ann) {$a_{nn}$};
\node[right=of b1]  (bn)  {$b_n$};
\node[right=of {$(ak2)!0.5!(ann)$}] (sigma) {$\Sigma$};
    \end{scope}

\path   ($(a21)!0.3!(an1)$) node (d11) [dot] {}
        ($(a21)!0.5!(an1)$) node (d12) [dot] {}
        ($(a21)!0.7!(an1)$) node (d13) [dot] {}; % changed
\path   ($(ak2)!0.3!(ann)$) node (d31) [dot] {}
        ($(ak2)!0.5!(ann)$) node (d32) [dot] {}
        ($(ak2)!0.7!(ann)$) node (d33) [dot] {}; % changed
\path   ($(d11)!0.5!(d31)$) node (d31) [dot] {}
        ($(d12)!0.5!(d32)$) node (d32) [dot] {}
        ($(d13)!0.5!(d33)$) node (d33) [dot] {}; % changed
\path   ($(a11)!0.5!(ak1)$) node (d20) [dot] {}
        ($(b1)!0.5!(bn)$)   node (d24) [dot] {}; % changed

\path   (a11) edge (d20) edge (ak1)
        (b1)  edge (d24) edge (bn);              % changed

\draw   (y1) edge ["$w_{11}^1$"] (a11)           % changed, used `quotes` library
        (y1) edge (a21)
        (y2) edge ["$w_{21}^1$"] (a21)
        (y2) edge (an1)

        (yn) edge (a11)
        (yn) edge ["$w_{11}^1$"] (an1)
        (yn) edge ["$w_{n1}^1$"] (b1);

\foreach \i in {a11, an1, b1}
    \draw[arr] (b) -- (\i);       % changed, used loops
\foreach \i in {ak1, ak2, ann, bn}
    \draw[arr] (\i) -- (sigma);   % changed, used loops
\draw[arr] (sigma) -- +(1,0) node[right]{$I$};
\end{tikzpicture}
\caption{In this example is assumed, that both pictures are in \texttt{figure} float with this caption.}
    \end{figure}
\lipsum[66]
\end{document}

在此处输入图片描述

顺便说一句,我强烈建议你阅读一些 LaTeX 介绍性文字,例如LATEX 的简单介绍以及至少第一个教程和第三部分“TikZ ist kein Zeichenprogramm”在 TiZ 包文档。

相关内容