![tikz 绘制的图片与页面边框之间出现不需要的空白](https://linux22.com/image/390279/tikz%20%E7%BB%98%E5%88%B6%E7%9A%84%E5%9B%BE%E7%89%87%E4%B8%8E%E9%A1%B5%E9%9D%A2%E8%BE%B9%E6%A1%86%E4%B9%8B%E9%97%B4%E5%87%BA%E7%8E%B0%E4%B8%8D%E9%9C%80%E8%A6%81%E7%9A%84%E7%A9%BA%E7%99%BD.png)
我在页面的角落画了一个斜带,但是图片和页面边框之间出现了多余的空白。我猜这是由节点文本引起的。但是为什么呢?有没有更好的方法来消除空白? 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}