tikz 绘制的图片与页面边框之间出现不需要的空白

tikz 绘制的图片与页面边框之间出现不需要的空白

我在页面的角落画了一个斜带,但是图片和页面边框之间出现了多余的空白。我猜这是由节点文本引起的。但是为什么呢?有没有更好的方法来消除空白? MWE:

\documentclass{article}
\usepackage{tikz,picture,eso-pic,calc}
\begin{document}
\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
      \AtPageLowerLeft{%
        \tikz\draw[fill=gray,draw=red,above,line width=#3]
        (0,#1)-- node[midway,sloped]{node text}
        (#1,0)--
        (#1+#2,0)--
        (0,#1+#2)--cycle;%
      }}}

\drawband{1cm}{1cm}{1pt}
first page\clearpage second page
\end{document}

在此处输入图片描述

答案1

我会在这里使用覆盖。(这使得规范变得\AtPageLowerLeft{不必要,但不会造成任何损害。)

\documentclass{article}
\usepackage{tikz,picture,eso-pic,calc}
\begin{document}
\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
      \AtPageLowerLeft{%
        \tikz[overlay,remember picture]{\draw[fill=gray,draw=red,above,line width=#3]
        ([yshift=#1]current page.south west)-- node[midway,sloped]{node text}
        ([xshift=#1]current page.south west)--
        ([xshift=#1+#2]current page.south west)--
        ([yshift=#1+#2]current page.south west)--cycle;}%
      }}}

\drawband{1cm}{1cm}{1pt}
first page\clearpage second page
\end{document}

在此处输入图片描述

此代码更短,但功能相同:

\documentclass{article}
\usepackage{tikz,eso-pic}
\begin{document}
\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
        \tikzlay,remember picture]{\draw[overlay,fill=gray,draw=red,above,line width=#3]
        ([yshift=#1]current page.south west)-- node[midway,sloped]{node text}
        ([xshift=#1]current page.south west)--
        ([xshift=#1+#2]current page.south west)--
        ([yshift=#1+#2]current page.south west)--cycle;}%
}}

\drawband{1cm}{1cm}{1pt}
first page\clearpage second page
\end{document}

请参阅章节17.13.2 引用当前页面节点 – 绝对定位请参阅 pgfmanual 以了解其工作原理的详细信息。

我个人可能会将命令改为

\documentclass{article}
\usepackage{tikz,eso-pic}
\begin{document}
\tikzset{band/.style={}}
\newcommand\drawband[3][line width=1pt]{%
  \AddToShipoutPictureBG{%
        \tikz[remember picture,overlay]{\draw[fill=gray,draw=red,above,#1]
        ([yshift=#2]current page.south west)--
        ([xshift=#2]current page.south west)--
        ([xshift=#2+#3]current page.south west)--
        ([yshift=#2+#3]current page.south west)--cycle;
        \path ([yshift=#2+#3/2]current page.south west) -- 
        node[midway,sloped]{node text}
         ([xshift=#2+#3/2]current page.south west);}%
}}

\drawband{1cm}{1cm}
first page\clearpage second page
\end{document}

在此处输入图片描述

这样,您就可以随时灵活地更改样式,并将文本置于带的中心。编辑:已添加remember picture到示例中,非常感谢@lyl!

答案2

问题是文本对于图片来说稍微太宽,请将其设置为零宽度框。

\documentclass{article}
\usepackage{tikz,picture,eso-pic,calc}
\begin{document}
\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
      \AtPageLowerLeft{%
        \tikz\draw[fill=gray,draw=red,above,line width=#3]
        (0,#1)-- node[midway,sloped]{\makebox[0pt]{node text}}
        (#1,0)--
        (#1+#2,0)--
        (0,#1+#2)--cycle;%
      }}}

\drawband{1cm}{1cm}{1pt}

\mbox{} % force a page to be produced

\end{document}

在此处输入图片描述

将文本放置在梯形中间的方法如下:

\documentclass{article}
\usepackage{tikz,picture,eso-pic,calc}

\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
    \AtPageLowerLeft{%
      \begin{tikzpicture}
      \draw[fill=gray,draw=red,above,line width=#3]
        (0,#1)--(#1,0)--(#1+#2,0)--(0,#1+#2)--cycle;
      \node at ({(#1*2+#2)/4},{(#1*2+#2)/4})[rotate=-45,inner sep=-#1]
      \end{tikzpicture}%
    }%
  }%
}

\begin{document}

\drawband{1cm}{1cm}{1pt}

\mbox{}

\end{document}

在此处输入图片描述

如果您想忽略上升部和下降部,请粉碎文本并插入幻像。

\documentclass{article}
\usepackage{tikz,picture,eso-pic,calc}

\newcommand\drawband[3]{%
  \AddToShipoutPictureBG{%
    \AtPageLowerLeft{%
      \begin{tikzpicture}
      \draw[fill=gray,draw=red,above,line width=#3]
        (0,#1)--(#1,0)--(#1+#2,0)--(0,#1+#2)--cycle;
      \node at ({(#1*2+#2)/4},{(#1*2+#2)/4})[rotate=-45,inner sep=-#1]
        {\smash{node text}\vphantom{x}};
      \end{tikzpicture}%
    }%
  }%
}

\begin{document}

\drawband{1cm}{1cm}{1pt}

\mbox{}

\end{document}

在此处输入图片描述

相关内容