我想用网格(图形)填充页面上段落之间的所有空间

我想用网格(图形)填充页面上段落之间的所有空间

我从另一个类似问题的答案中举了一个例子(如何定义图形大小以使其占据页面的剩余部分?)。

我想要在试卷的一页上显示以下内容:

问题

四方纸(浅灰色)

也许是“检查你的答案”这样的评论。

以下是我的 MWE:

\documentclass[svgnames,a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[margin=25mm]{geometry}
\usepackage{lipsum}

\newcommand{\currentsidemargin}{%
\ifodd\value{page}%
   \oddsidemargin%
\else%
    \evensidemargin%
\fi%
}

\begin{document}
\setlength{\parindent}{0mm} \setlength{\parskip}{6pt}
\newsavebox\mybox
\savebox{\mybox}[\textwidth][c]{  % The text for below the grid
 \parbox{\textwidth}{
    \textbf{\Huge Some text}

    \lipsum[3]
  }
}

\newlength\gnat
\settoheight{\gnat}{\usebox{\mybox}}
\addtolength{\gnat}{0.5\parskip}

\lipsum[2-5]% dummy text

\begin{tikzpicture}[overlay,remember picture]

% Helper nodes
  \path (current page.north west) ++(\hoffset, -\voffset)
    node[anchor=north west, shape=rectangle, inner sep=0, minimum width=\paperwidth, 
    minimum height=\paperheight] (pagearea) {};

 \path (pagearea.north west) ++(\leftmargin+\currentsidemargin,
    -\topmargin-\headheight-\headsep+\gnat) 
     node[anchor=north west, shape=rectangle, inner sep=0, 
     minimum width=\textwidth, minimum height=\textheight] (textarea) {};

% Image
 \path let \p0 = (0,0), \p1 = (textarea.south) in
    node [inner sep=0pt,outer sep=0pt,anchor=north west] {%
    \pgfmathsetmacro\imgheight{(\y0-\y1-0.5\gnat-mod(\y0-\y1-0.5\gnat,8mm))}%
    \begin{tikzpicture}[remember picture,overlay]
         \node[xshift=+0mm,yshift=1mm] at (\p0){
         \begin{tikzpicture}%[overlay,remember picture]
             \tikzset{dotted lines/.style={black, loosely dotted,  thick}} 
             \draw[style=dotted lines,step=.8cm] (0,0) grid (160mm,\imgheight pt+24mm);
          \end{tikzpicture}
         };
    \end{tikzpicture}
    %}
 };
\end{tikzpicture}
\vfill
\vfill
\usebox{\mybox}
%\the\gnat   % To see the space
\end{document}

我的问题:为什么我必须添加24 毫米到网格以使其填满整个可用空间?

[我稍后会添加一个测试来检查框是否为空,然后将其设置\gnat为零。尽管任何建议都会受到赞赏。]

答案1

您的辅助节点的构造似乎存在问题。但是,这些问题可以轻松避免:

\documentclass[svgnames,a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{dotted lines/.style={black, loosely dotted,  thick}}
\usepackage[margin=25mm]{geometry}
\usepackage{lipsum}

\newcommand\drawGrid{%
    \tikz[remember picture,overlay] \node[inner sep=0,anchor=base] (tl) {};%
    \vfill\hfill%
    \tikz[remember picture,overlay] \coordinate (br);%
    \tikz[remember picture,overlay] 
        \draw[dotted lines,step=.8cm] let \p1=(tl.north), \p2=(br) in
            [yshift={mod(\y1-\y2,8mm)},xshift=\x1]      % move (0,0) to the bottom left and nudge it a bit upward to get full squares.
            (0,0) grid  
            ({\x2-\x1-mod(\x2-\x1,8mm)+0.1pt},{\y1-mod(\y1-\y2,8mm)+0.1pt});    % the +0.1pt are just there to avoid any rounding problems.
}

\begin{document}
\setlength{\parindent}{0mm} \setlength{\parskip}{6pt}

\lipsum[2-5]% dummy text

\drawGrid

\textbf{\Huge Some text}

\lipsum[3]\clearpage

\lipsum[1-2]

\drawGrid

abc
\clearpage
\end{document}

答案2

这是一个“无 TikZ”的解决方案

\documentclass[a4paper]{article}
\usepackage[textwidth=16cm]{geometry}
\usepackage{lipsum,array}

\def\leadAbox{\hbox to 1.6mm{\smash{\tiny.}\hfil}}
\def\start{\rlap{\hbox to 16.2cm{\leaders\leadAbox\hfill}}}
\def\leadBbox{\hbox to 8mm{\smash{\tiny.}\hfil\vrule height1mm width 0pt}}
\def\squares{\rlap{\hbox to 16.8cm{\leaders\leadBbox\hfill}}}
\def\leadCbox{\start\kern-.4mm
  \vbox to 8mm{\offinterlineskip\leaders\squares\vfill}
  \nointerlineskip\start}
\def\gridfill{\par\leaders\vbox{\leadCbox}\vfill}
\begin{document}

\lipsum[2]

\gridfill

\lipsum[2]

\end{document}

相关内容