Tikz 独立版的背景是透明的,使其变为白色

Tikz 独立版的背景是透明的,使其变为白色

我正在使用下面的 Latex 代码来绘制tikzpicture

\documentclass[convert={density=300,size=1080x800,outext=.png},tikz, border=1cm]{standalone}
\usepackage{{xcolor}}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}

    \draw node[draw, fill={{rgb:black,1;white,5}}, minimum size=4cm,line width=0.1cm,font=\fontsize{100}{100}\selectfont, label={[yshift=-6cm,style={font=\fontsize{100}{100} } ] {$x_9$} } ] at (25, 20) {1};
    \draw [line width=0.1cm](25,22.0) -- (25,24.0);
    \draw [line width=0.12cm] (25,25.0) circle (1);
    \draw [line width=0.12cm](25,24.0) -- (25,26.0);
    \draw [line width=0.12cm](24,25.0) -- (26,25.0);
    \draw [arrows={-Triangle[angle=90:0.5cm,black,fill=black,line width=0.1cm]}](26,25.0) -- (25.9999,25.0);

\end{tikzpicture}
\end{document}

该文件被编译pdflatex -shell-escape file.tex并生成下面的 png。

在此处输入图片描述

虽然这里看不到,但是 png 有一个透明的背景(你只需查看图像就可以看到它关联)。我希望它直接具有白色背景,而不是使用额外的外部工具进行转换。

我们怎样才能同时拥有白色背景或任意颜色?

答案1

您可以使用该backgrounds库。请注意,此解决方案假设您不将该background层用于其他目的,如果您这样做,您可能必须引入其他层并对其进行适当排序。

\documentclass[convert={density=300,size=1080x800,outext=.png},tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta,backgrounds}
\tikzset{white background/.style={show background rectangle,tight background,
background rectangle/.style={fill=white}}}
\begin{document}
\begin{tikzpicture}[white background]

    \draw node[draw, fill={{rgb:black,1;white,5}}, minimum size=4cm,line width=0.1cm,font=\fontsize{100}{100}\selectfont, label={[yshift=-6cm,style={font=\fontsize{100}{100} } ] {$x_9$} } ] at (25, 20) {1};
    \draw [line width=0.1cm](25,22.0) -- (25,24.0);
    \draw [line width=0.12cm] (25,25.0) circle (1);
    \draw [line width=0.12cm](25,24.0) -- (25,26.0);
    \draw [line width=0.12cm](24,25.0) -- (26,25.0);
    \draw [arrows={-Triangle[angle=90:0.5cm,black,fill=black,line width=0.1cm]}](26,25.0) -- (25.9999,25.0);

\end{tikzpicture}
\end{document}

答案2

如果您正在制作独立图像并且不只是希望您的 tikzpicture 具有背景,那么使用可能会更简单\pagecolor

例如以下代码:

\documentclass[convert={density=300,size=1080x800,outext=.png},tikz, border=1cm]{standalone}
\usepackage{{xcolor}}
\usetikzlibrary{arrows.meta}

\begin{document}
\pagecolor{cyan}     % This line here
\begin{tikzpicture}

    \draw node[draw, fill={{rgb:black,1;white,5}}, minimum size=4cm,line width=0.1cm,font=\fontsize{100}{100}\selectfont, label={[yshift=-6cm,style={font=\fontsize{100}{100} } ] {$x_9$} } ] at (25, 20) {1};
    \draw [line width=0.1cm](25,22.0) -- (25,24.0);
    \draw [line width=0.12cm] (25,25.0) circle (1);
    \draw [line width=0.12cm](25,24.0) -- (25,26.0);
    \draw [line width=0.12cm](24,25.0) -- (26,25.0);
    \draw [arrows={-Triangle[angle=90:0.5cm,black,fill=black,line width=0.1cm]}](26,25.0) -- (25.9999,25.0);

\end{tikzpicture}
\end{document}

生成该图像的方法如下pdflatex --shell-escape main.tex

在纯青色背景上绘制的黑色和灰色 LFSR

但是,请注意,这\pagecolor似乎会弄乱独立或其他设备的自动调整大小功能,并且您的图像可能比预期的稍大,并且具有小的透明边缘。

相关内容