
問題
如何使網格線完美對齊,使其與頁面上的物理毫米相對應。
情況
我有一張A4紙。我想在其上繪製網格線,並標記每一毫米(顯然編號尺寸用於數位用途,實際上厘米將標記為實體列印)。
標準
- 適用於任何紙張尺寸(只要
\step
調整了最大值) - 線條與物理尺寸對齊
- 覆蓋層將覆蓋任何其他 tikz 圖片(或其他任何內容),無論在文件中的何處呼叫該命令(我可以在需要網格的任何頁面上呼叫該命令)
問題
- 線條並非從原點開始
- 無論在何處調用,網格都不位於
tikzpicture
文件中調用的上方。\showgrid
範例程式碼
\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{anyfontsize}
\newcommand{\showgrid}{%
\begin{tikzpicture}[overlay,remember picture,every node/.style={inner sep=0pt,outer sep=0pt}]%
\draw[help lines,xstep=1mm,ystep=1mm,gray!25] (current page.north west) grid (current page.south east);
\draw[help lines,xstep=10mm,ystep=10mm,color=gray] (current page.south west) grid (current page.north east);
\foreach \step in {0,1,...,297} {
\node [anchor=north] at ($ (current page.north west) + (\step mm,0cm) $) {\fontsize{1}{2}\selectfont \step};
\node [anchor=west] at ($ (current page.north west) + (0cm,-\step mm) $) {\fontsize{1}{2}\selectfont \step};
}
\end{tikzpicture}
}%
\begin{document}
\thispagestyle{empty}
\showgrid{}
\begin{tikzpicture}[overlay,remember picture,every node/.style={fill=red,inner sep=0pt,outer sep=0pt}]%
\node [minimum width=2cm,minimum height=2cm] at (current page.center) {};
\end{tikzpicture}%
\end{document}
輸出
更新:我的解決方案
這是一種混合解決方案,利用 cfr 的方法與 esdd 答案的自動頁面大小檢測相結合。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usetikzlibrary{backgrounds}
\usepackage{anyfontsize}
\usepackage{atbegshi}
\newcommand{\showgrid}{%
\AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
\begin{tikzpicture}
[
overlay,
remember picture,
inner sep=0pt,
outer sep=0pt,
minor line/.style={help lines, draw=black!50, on background layer},
major line/.style={help lines, draw=black},
major number/.style={font=\fontsize{3}{5}\selectfont\bfseries},
minor number/.style={font=\fontsize{1}{2}\selectfont},
]
\pgfmathtruncatemacro\xmaxstep{\paperwidth/1mm}% calculate needed steps in x direction
\pgfmathtruncatemacro\ymaxstep{\paperheight/1mm}% calculate needed steps in y direction
\foreach \step in {0,1,...,\xmaxstep} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
}
\foreach \step in {0,1,...,\ymaxstep} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\pgfmathsetmacro\numberconfig{ifthenelse(equal(int(mod(\step,10)),0),"major number","minor number")}%
\draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
\node [anchor=north,\numberconfig] at ($ (current page.north west) + (\step mm,0) $) {\step};
\node [anchor=west,\numberconfig] at ($ (current page.north west) - (0,\step mm) $) {\step};
}
\end{tikzpicture}
}%
}%
}
\tikzset{%
myseg/.style={%
red,very thick
}
}
\begin{document}
\null
\showgrid
\begin{tikzpicture}[overlay,remember picture]
\draw [myseg] (current page text area.north west) -- (current page text area.north east) -- (current page text area.south east) -- (current page text area.south west) -- (current page text area.north west);
\draw [myseg] (current page header area.north west) -- (current page header area.north east) -- (current page header area.south east) -- (current page header area.south west) -- (current page header area.north west);
\draw [myseg] (current page footer area.north west) -- (current page footer area.north east) -- (current page footer area.south east) -- (current page footer area.south west) -- (current page footer area.north west);
\draw [myseg] (current page marginpar area.north west) -- (current page marginpar area.north east) -- (current page marginpar area.south east) -- (current page marginpar area.south west) -- (current page marginpar area.north west);
\end{tikzpicture}
\end{document}
輸出
筆記:當對佈局進行逆向工程時,可以使用大多數列印功能表上的「縮放到印表機邊距」選項來將該網格列印在另一個PDF 上,因為PDF 和網格都將以相同的比例縮放,這意味著縮小的毫米將相當於底層 PDF 上的一毫米。可以在此處找到使用此程式碼在圖像上繪圖的範例:https://tex.stackexchange.com/a/269156/13552
答案1
請注意,在發布的 PNGS 中,線寬可能看起來不均勻。這是小螢幕/PDF 檢視器組合的產物,與實際的 PDF 無關。它只會影響我的 PNG 剪報。
我認為這個解決方案滿足了各種需求:
- 可以透過調整 s 的定義來適應其他紙張尺寸
\step
。 - 紙張的西北角位於原點,網格線與頁面的物理尺寸對齊,即網格的小正方形和大正方形的西北角與北對齊- 頁面的西角。
- 網格覆蓋其他頁面內容,包括
tikzpicture
s,即使這些內容使用overlay, remember picture
它們自己。 \showgrid
可以在頁面上需要網格的任何位置指定,包括在任何tikzpicture
s 之前,即使它們本身使用overlay, remember picture
.- 網格僅顯示在請求的頁面上。下一頁將不使用網格。 (但是很容易對其進行調整,以便它顯示在每個頁面或其他地方。請參閱 的文檔
atbegshi
。)
我用手畫了網格,分別畫了水平線和垂直線。我使用該backgrounds
庫來確保較淺的線條不會繪製在較暗的線條上(這看起來相當奇怪)。
我用來atbegshi
確保網格放置在所有頁面內容之上。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{backgrounds}
\usepackage{anyfontsize}
\newcommand{\showgrid}{%
\AtBeginShipoutNext{\AtBeginShipoutAddToBoxForeground{%
\begin{tikzpicture}
[
overlay,
remember picture,
inner sep=0pt,
outer sep=0pt,
minor line/.style={help lines, draw=gray!25, on background layer},
major line/.style={help lines, draw=gray},
]
\foreach \step in {0,...,210} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\draw [\gridlineconfig] ($(current page.north west) + (\step mm,0)$) -- ($(current page.south west) + (\step mm,0)$);
}
\foreach \step in {0,...,297} {
\pgfmathsetmacro\gridlineconfig{ifthenelse(equal(int(mod(\step,10)),0),"major line","minor line")}%
\draw [\gridlineconfig] ($(current page.north west) - (0,\step mm)$) -- ($(current page.north east) - (0,\step mm)$);
\node [anchor=north] at ($ (current page.north west) + (\step mm,0) $) {\fontsize{1}{2}\selectfont \step};
\node [anchor=west] at ($ (current page.north west) - (0,\step mm) $) {\fontsize{1}{2}\selectfont \step};
}
\end{tikzpicture}
}%
}%
}
\usepackage{atbegshi}
\begin{document}
\thispagestyle{empty}
\showgrid
\begin{tikzpicture}[overlay,remember picture,every node/.style={fill=red,inner sep=0pt,outer sep=0pt}]%
\node [minimum width=2cm,minimum height=2cm] at (current page.center) {};
\end{tikzpicture}
\end{document}
答案2
\AddToShipoutPictureFG*
包定義的命令eso-pic
將其內容設定在目前頁面的頂部。此外,還可以根據紙張尺寸計算 x 和 y 方向上所需的標籤數量。
\documentclass[a5paper]{article}
%\usepackage{fontspec} % commented to speed up compilation
\usepackage{blindtext}% dummy text
\usepackage{tikz}
\usepackage{anyfontsize}
\usepackage{eso-pic}
\newcommand{\showgrid}{%
\AddToShipoutPictureFG*{%
\begin{tikzpicture}[overlay,remember picture,
thin,nodes={font=\fontsize{1}{2}\selectfont},
yshift=\paperheight% origin is in the upper left corner
]
\draw[gray!25,step=1mm](current page.south west)grid(current page.north east);
\draw[blue!30!gray,step=10mm](current page.south west) grid(current page.north east);
\pgfmathtruncatemacro\xmaxstep{\paperwidth/1mm}% calculate needed steps in x direction
\pgfmathtruncatemacro\ymaxstep{\paperheight/1mm}% calculate needed steps in y direction
\foreach \step in {0,1,...,\xmaxstep}
\node [anchor=north] at ([xshift=\step mm]current page.north west) {\step};
\foreach \step in {0,1,...,\ymaxstep}
\node [anchor=west] at ([yshift=-\step mm]current page.north west) {\step};
% \node[fill=red]at(0,0){};% to show the origin
\end{tikzpicture}%
}%
}
\begin{document}
\blindtext[2]
\showgrid
\tikz[overlay,remember picture]\node[minimum size=2cm,fill=red] at (current page.center) {};
\blinddocument
\showgrid
\blinddocument
\end{document}
使用套件,scrlayer
您可以將網格定義為新的頁面樣式。
\documentclass[a5paper]{article}
%\usepackage{fontspec} % commented to speed up compilation
\usepackage{blindtext}% dummy text
\usepackage{tikz}
\usepackage{anyfontsize}
\usepackage{scrlayer}
\DeclareNewLayer[foreground,page,
contents={%
\begin{tikzpicture}[thin,nodes={font=\fontsize{1}{2}\selectfont}]
\useasboundingbox(0,0)rectangle(\layerwidth,-\layerheight);
\draw[gray!25,step=1mm](0,0)grid(\layerwidth,-\layerheight);
\draw[orange!30!gray,step=10mm](0,0)grid(\layerwidth,-\layerheight);
\pgfmathtruncatemacro\xmaxstep{\layerwidth/1mm}% calculate needed steps in x direction
\pgfmathtruncatemacro\ymaxstep{\layerheight/1mm}% calculate needed steps in y direction
\foreach \step in {0,1,...,\xmaxstep}
\node [anchor=north] at (\step mm,0) {\step};
\foreach \step in {0,1,...,\ymaxstep}
\node [anchor=west] at (0,-\step mm){\step};
\end{tikzpicture}%
}
]{grid.fg}
\DeclareNewPageStyleByLayers{grid}{grid.fg}
\pagestyle{headings}
\begin{document}
\blindtext[2]
\thispagestyle{grid}
\tikz[overlay,remember picture]\node[minimum size=2cm,fill=red] at (current page.center) {};
\blinddocument
\thispagestyle{grid}
\blinddocument
\end{document}
答案3
部分解決方案
下面的程式碼有一些小問題。
網格的原點位於頁面的左下角(OP想要左上角)
使用
\AddToShipoutPicture
使原點為 1. 但也使網格位於頁面內容下方。移除它,網格就會崩潰,但起源卻變得瘋狂。
和\AddToShipoutPicture
沒有\AddToShipoutPicture
分子量(?)E
\documentclass[a5paper]{article} % a5 just to example
%\usepackage{fontspec} % commented to speed up compilation
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{calc}
\usepackage{anyfontsize}
\usepackage{eso-pic}
\newcommand{\showgrid}{%
% \AddToShipoutPicture{%
\begin{tikzpicture}[overlay,remember picture]
\draw[blue!30!white]
(current page.south west) grid[step=1mm]
(current page.north east);
\draw[blue!80!white]
(current page.south west) grid[step=10mm]
(current page.north east);
\foreach \step in {0,1,...,297} {
\node [anchor=north] at ($ (current page.north west) + (\step mm,0cm) $) {\fontsize{1}{2}\selectfont \step};
\node [anchor=west] at ($ (current page.north west) + (0cm,-\step mm) $) {\fontsize{1}{2}\selectfont \step};
}
\end{tikzpicture}
% }%
}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[overlay,remember picture,every node/.style={fill=red,inner sep=0pt,outer sep=0pt}]%
\node [minimum width=2cm,minimum height=2cm] at (current page.center) {};
\end{tikzpicture}%
\showgrid
\end{document}
答案4
這裡有一個使用 afterpage 套件並重新定義 \@outputpage 命令的解決方案。也許有人可以使用 etoolbox 修補此命令。它不使用當前頁面。
\documentclass{article}
\usepackage{tikz}
\usepackage{afterpage}
\usetikzlibrary{calc}
\usepackage{anyfontsize}
\makeatletter
\newcommand{\showgrid}{%
\let\grid@outputpage\@outputpage
\def\@outputpage{%
\begingroup % the \endgroup is put in by \aftergroup
\let \protect \noexpand
\@resetactivechars
\global\let\@@if@newlist\if@newlist
\global\@newlistfalse
\@parboxrestore
\shipout \vbox{%
\set@typeset@protect
\aftergroup \endgroup
\aftergroup \set@typeset@protect
% correct? or just restore by ending
% the group?
\if@specialpage
\global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
\fi
\if@twoside
\ifodd\count\z@ \let\@thehead\@oddhead \let\@thefoot\@oddfoot
\let\@themargin\oddsidemargin
\else \let\@thehead\@evenhead
\let\@thefoot\@evenfoot \let\@themargin\evensidemargin
\fi
\fi
\reset@font
\normalsize
\normalsfcodes
\let\label\@gobble
\let\index\@gobble
\let\glossary\@gobble
\baselineskip\z@skip \lineskip\z@skip \lineskiplimit\z@
\@begindvi
\vskip \topmargin
\moveright\@themargin \vbox {%
\setbox\@tempboxa \vbox to\headheight{%
\vfil
\color@hbox
\normalcolor
\hb@xt@\textwidth{\@thehead}%
\color@endbox
}% %% 22 Feb 87
\dp\@tempboxa \z@
\box\@tempboxa
\vskip \headsep
\box\@outputbox
\baselineskip \footskip
\color@hbox
\normalcolor
\hb@xt@\textwidth{\@thefoot}%
\color@endbox
}%
\vskip-\dimexpr\textheight+\topmargin+\headheight+\headsep+\footskip+1in\relax%
\hspace*{-1in}%
\begin{tikzpicture}[every node/.style={inner sep=0pt,outer sep=0pt}]%
\draw[help lines,gray!25] (0,0) grid[step=1mm] (\paperwidth,-\paperheight);
\draw[help lines,gray] (0,0) grid[step=10mm] (\paperwidth,-\paperheight);
\foreach \step in {0,1,...,297} {
\node [anchor=north] at ($ (0,0) + (\step mm,0cm) $) {\fontsize{1}{2}\selectfont \step};
\node [anchor=west] at ($ (0,0) + (0cm,-\step mm) $) {\fontsize{1}{2}\selectfont \step};
}
\end{tikzpicture}
}%
\global\let\if@newlist\@@if@newlist
\global \@colht \textheight
\stepcounter{page}%
\let\firstmark\botmark
}\afterpage{\global\let\@outputpage\grid@outputpage}}
\makeatother
\begin{document}
\thispagestyle{empty}
test
\showgrid
and test
\begin{tikzpicture}[overlay,remember picture,every node/.style={fill=red,inner sep=0pt,outer sep=0pt}]%
\node [minimum width=2cm,minimum height=2cm] at (current page.center) {};
\end{tikzpicture}%
\newpage
test
\newpage
test
\showgrid
and test
\end{document}