如何消除独立图像的左右边距

如何消除独立图像的左右边距

当使用该standalone包将我的图像移出主文档时
,会出现令人讨厌的副作用。独立 tex 文件的左边距始终约为 0.75 厘米。
我可以使用\useasboundingbox(0.8,0) (4.1,4.6);tikz 代码手动删除它,也可以通过lmargin明确设置使用 来删除它\usepackage[margin=1in, lmargin=1.75cm, rmargin=1.75cm]{geometry}

第一个选项 ( \useasboundingbox) 比较繁琐,因为我必须手动找出每张图片的正确调整。
第二个选项 ( \usepackage{geometry}) 会在之前没有边框的地方添加一个右边距,同样需要手动重新调整每张图片。

以下是相关文件:

% Tikz File 'mytikz.tex'
\documentclass{standalone}
\usepackage{standalone}
%\usepackage[margin=1in, lmargin=1.75cm, rmargin=1.75cm]{geometry}
\standaloneconfig{mode=image|tex}
\usepackage{tikz}
%\usetikzlibrary{...}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{arrows.meta,calc}
\begin{document}
  \definecolor{light-gray}{gray}{0.98}
  \definecolor{darkgreen}{RGB}{0,64,0}
  \definecolor{lightgray}{gray}{0.75}
  \definecolor{lightred}{RGB}{255,200,200} 
  \definecolor{lightblue}{RGB}{200,200,255}
  \definecolor{lightgreen}{RGB}{200,255,200}
  \definecolor{pink}{RGB}{255,128,128}
    \begin{tikzpicture} 
    %\useasboundingbox(0.8,0) (4.1,4.6);
    \def\opacity{0.75}
    \def\xcellsa{{"A","B","C","D"}}

    \def\xcellsb{{"4","3","2","1"}}

    %bc slice
    \filldraw[lightblue, opacity=0.5] (1,0) rectangle (3,4);
    \draw[step=1,black, opacity=\opacity, fill opacity=0] (1,0) grid (3,4);
    %23slice
    \filldraw[lightblue, opacity=0.5] (0,1) rectangle (4,3);
    \draw[step=1,black, opacity=\opacity, fill opacity=0] (0,1) grid (4,3);
    %core
    \draw[fill = white, thick, dashed] (2,2) circle (0.5cm);
    \node[fill = white, opacity=1] at (2,2) {core};
    %coordinate
    \draw[fill = black] (1.5,2.6) circle (0.1cm);
    \node at (1.5, 2.6) (A) {};
    \node at (0.8,4.4) (B) {};
    \draw[-Stealth, semithick] (B) to [bend right] (A);
    \node[fill=white, opacity=1] at (1,4.4) {anchor};

    \foreach \xa in {1,2} {
      \node[align=center] at ({\xa+0.5},{4-0.5}) {\pgfmathparse{\xcellsa[\xa]}\pgfmathresult};
    }
    \foreach \ya in {1,2} {  
      \node[align=center] at ({0.5},{\ya+0.5}) {\pgfmathparse{\xcellsb[\ya]}\pgfmathresult};
    }  
  \end{tikzpicture}
\end{document}

我如何才能让它自动呈现无边距(或非常小的边距)?

答案1

根据 Ulrike Fischer 的建议,我将颜色定义移至序言。

这样就解决了问题。图像现在没有边框了:

% Tikz File 'mytikz.tex'
\documentclass{standalone}
\usepackage{standalone}
%\usepackage[margin=1in, lmargin=1.75cm, rmargin=1.75cm]{geometry}
\standaloneconfig{mode=image|tex}
\usepackage{tikz}
%\usetikzlibrary{...}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{arrows.meta,calc}
\definecolor{light-gray}{gray}{0.98}
\definecolor{darkgreen}{RGB}{0,64,0}
\definecolor{lightgray}{gray}{0.75}
\definecolor{lightred}{RGB}{255,200,200} 
\definecolor{lightblue}{RGB}{200,200,255}
\definecolor{lightgreen}{RGB}{200,255,200}
\definecolor{pink}{RGB}{255,128,128}
\begin{document}
    \begin{tikzpicture} 
    %\useasboundingbox(0.8,0) (4.1,4.6);
    \def\opacity{0.75}
    \def\xcellsa{{"A","B","C","D"}}
    
    \def\xcellsb{{"4","3","2","1"}}
    
    %bc slice
    \filldraw[lightblue, opacity=0.5] (1,0) rectangle (3,4);
    \draw[step=1,black, opacity=\opacity, fill opacity=0] (1,0) grid (3,4);
    %23slice
    \filldraw[lightblue, opacity=0.5] (0,1) rectangle (4,3);
    \draw[step=1,black, opacity=\opacity, fill opacity=0] (0,1) grid (4,3);
    %core
    \draw[fill = white, thick, dashed] (2,2) circle (0.5cm);
    \node[fill = white, opacity=1] at (2,2) {core};
    %coordinate
    \draw[fill = black] (1.5,2.6) circle (0.1cm);
    \node at (1.5, 2.6) (A) {};
    \node at (0.8,4.4) (B) {};
    \draw[-Stealth, semithick] (B) to [bend right] (A);
    \node[fill=white, opacity=1] at (1,4.4) {anchor};
   
    \foreach \xa in {1,2} {
      \node[align=center] at ({\xa+0.5},{4-0.5}) {\pgfmathparse{\xcellsa[\xa]}\pgfmathresult};
    }
    \foreach \ya in {1,2} {  
      \node[align=center] at ({0.5},{\ya+0.5}) {\pgfmathparse{\xcellsb[\ya]}\pgfmathresult};
    }  
  \end{tikzpicture}
\end{document}

前:在此处输入图片描述后:在此处输入图片描述

相关内容