独立的 tikz 在左侧有边框

独立的 tikz 在左侧有边框

我一直认为独立版会将我的图片置于中心。但是下面的例子却不是:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\usetikzlibrary{backgrounds}

\begin{document}
%jpg from http://www.murderousmaths.co.uk/magic/predictor%20cards%20template%20120.jpg
\def\infilename{/Users/e12714/Downloads/a.jpg}

\newsavebox{\graph}\savebox{\graph}{\includegraphics{\infilename}}
\newlength\gh\setlength\gh{\heightof{\usebox\graph}}
\newlength\gw\setlength\gw{\widthof{\usebox\graph}}

\newcounter{DEBUG}
\setcounter{DEBUG}{1}
\setcounter{DEBUG}{0}

\def\BL{0.16\gw,0.106\gh} % bottom left corner
\def\TR{0.92\gw,0.86\gh} % top right corner

\begin{tikzpicture}[framed,>=latex,
  image/.style={anchor=south west,inner sep=0,outer sep=0}]
    \ifthenelse{\theDEBUG = 1}{   
        % do nothing for debug purpose
    }{
        \clip (\BL) rectangle (\TR);
    }
   \node[image] (NI) {\usebox\graph};
   \ifthenelse{\theDEBUG = 1}{
        \draw[red,line width=2pt] (\BL) rectangle (\TR);
        \begin{scope}[x={(NI.south east)},y={(NI.north west)}]
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        \end{scope}
    }{}
\end{tikzpicture}

\end{document}

为什么剪辑后的图片左边还有一条白条? 在此处输入图片描述

答案1

发生这种情况的原因是实际图像之前有段落和其他空白。

如果将 移到\begin{document}之前\begin{tikzpicture},问题就会消失,因为序言中的空格会被忽略。

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\usetikzlibrary{backgrounds}

%jpg from http://www.murderousmaths.co.uk/magic/predictor%20cards%20template%20120.jpg
\def\infilename{example-image}

\newsavebox{\graph}\savebox{\graph}{\includegraphics{\infilename}}
\newlength\gh\setlength\gh{\heightof{\usebox\graph}}
\newlength\gw\setlength\gw{\widthof{\usebox\graph}}

\newcounter{DEBUG}
\setcounter{DEBUG}{1}
\setcounter{DEBUG}{0}

\def\BL{0.16\gw,0.106\gh} % bottom left corner
\def\TR{0.92\gw,0.86\gh} % top right corner

\begin{document}
\begin{tikzpicture}[framed,>=latex,
  image/.style={anchor=south west,inner sep=0,outer sep=0}]
    \ifthenelse{\theDEBUG = 1}{   
        % do nothing for debug purpose
    }{
        \clip (\BL) rectangle (\TR);
    }
   \node[image] (NI) {\usebox\graph};
   \ifthenelse{\theDEBUG = 1}{
        \draw[red,line width=2pt] (\BL) rectangle (\TR);
        \begin{scope}[x={(NI.south east)},y={(NI.north west)}]
            \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1.001,1.001);
            \foreach \x in {1,...,9} { \node [anchor=north] at (\x/10,0) {\x};}
            \foreach \y in {1,...,9} { \node [anchor=east] at (0,\y/10) {\y};}
        \end{scope}
    }{}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者,您可以%在空行中以及导致问题的行尾放置符号,但在这种情况下这不是必需的。

相关内容