僅產生圖形而不是 tikz 圖片的整個頁面

僅產生圖形而不是 tikz 圖片的整個頁面

我只想製作圖形而不是周圍的頁面。本質上,我想要圖形周圍有一個白色的框,但不是整個頁面的價值。

我發現這個答案,我可以根據 Peter Grill 的答案編譯 MWE。我知道我應該將 \documentclass 更改為獨立;但是,我無法使答案適用於我的範例。它會產生一個空白的 pdf(不是白頁,什麼都沒有)。這是一個修改版本這個 MLP 的例子,有兩個隱藏層而不是只有一個:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,transform canvas={scale=1.5}]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{hidden2 neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]

% Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {};

% Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

 % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden2 neuron] (H2-\name) at (\layersep*2,-\y cm) {};

% Draw the output layer nodes
      \foreach \name / \y in {1,...,3}
        \path[yshift=0.5cm]
            node[output neuron,pin=right:Class \#\y] (O-\name) at (\layersep*3,-\y cm) {};

% Connect every node in the input layer with every node in the
% hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,5}
            \path (I-\source) edge (H-\dest);

 % Connect every node in the first hidden layer with every node in the
% second hidden layer.
    \foreach \source in {1,...,5}
        \foreach \dest in {1,...,5}
            \path (H-\source) edge (H2-\dest);

% Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,5}
        \foreach \dest in {1,...,3}
            \path (H2-\source) edge (O-\dest);

% Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer 1};
    \node[annot,above of=H2-1, node distance=1cm] (hl) {Hidden layer 2};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
\end{document}

這會產生一個帶有圖片的頁面。我想要一張圖片大小的頁面。我怎樣才能做到這一點?

答案1

這裡有兩種可能,要么切換到獨立類,由於某種原因不起作用,明天測試。或確保沒有頁首或頁尾,即添加

\pagestyle{empty} 

到序言並重新編譯。然後在終端、dos 提示符或系統提供的任何內容中,透過 pdfcrop 運行生成的 pdf

pdfcrop file.pdf

這將在名稱下建立文件的裁剪版本file-crop.pdf


編輯:在使用類別測試程式碼後standalone,如果刪除以下內容,則程式碼可以運作

transform canvas={scale=1.5}

答案2

來自25.4 Canvas Transformations文件TikZ

畫布變換應該要非常小心地使用。在大多數情況下,您不希望圖片中的線寬發生變化,因為這會造成視覺不一致。

同樣重要的是,當您使用畫布變換時,pgf 會丟失節點位置和圖片大小的跟踪,因為它在計算節點坐標時沒有考慮畫布變換的影響(但是,不要依賴於此;它可能未來會改變) 。

最後,請注意,畫布變換始終應用於整個路徑,不可能(對於座標變換)在路徑的不同部分使用不同的變換。

簡而言之,除非您真正知道自己在做什麼,否則不應使用畫布轉換。

你讀了嗎pgf loses track ... of picture sizes?這是article更改後出現的問題standalone。正如transform canvas={scale=1.5}已應用於tikzpicture(不僅僅是特定的path),standalone無法計算正確的圖形大小,因為pgf不知道它。

一旦transform canvas={scale=1.5}被註解掉,standalone就再次裁剪圖形:

\documentclass[border=2mm]{standalone} %<--- standalone
\usepackage{tikz}

\begin{document}

\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,
    %transform canvas={scale=1.5} <----- Comment out
]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{hidden2 neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]

% Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {};

% Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

 % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden2 neuron] (H2-\name) at (\layersep*2,-\y cm) {};

% Draw the output layer nodes
      \foreach \name / \y in {1,...,3}
        \path[yshift=0.5cm]
            node[output neuron,pin=right:Class \#\y] (O-\name) at (\layersep*3,-\y cm) {};

% Connect every node in the input layer with every node in the
% hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,5}
            \path (I-\source) edge (H-\dest);

 % Connect every node in the first hidden layer with every node in the
% second hidden layer.
    \foreach \source in {1,...,5}
        \foreach \dest in {1,...,5}
            \path (H-\source) edge (H2-\dest);

% Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,5}
        \foreach \dest in {1,...,3}
            \path (H2-\source) edge (O-\dest);

% Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer 1};
    \node[annot,above of=H2-1, node distance=1cm] (hl) {Hidden layer 2};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
\end{document}

在此輸入影像描述

雖然結果不是scaled,但我明白可以做類似的事情

\includegraphics[scale=1.5]{...}

以獲得與 等效的結果transform canvas={scale=1.5}

相關內容