3

I'm currently using a command (adapted from https://tex.stackexchange.com/a/241139) to make horizontal lines repeat for my exams, and I am having trouble adding incrementing numbers to each line. I added a counter, but I'm guessing that leaders only evaluate the line one time and then simply repeat it, and so the counter incrementation is never done.

Is there a way to modify the command to actually make the numbers go up while still keeping the ability to repeat the lines until the end of the page? Here is the current code with all frames shown for spacing (the command is used as \fillwithlines{\vfill}). I should add that any solution that produces the same result exactly is perfectly fine. Tikz solutions that have the same use are accepted.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{ulem}
\usepackage{showframe}
\usepackage{changepage}
\makeatletter
\newenvironment{restoretext}% Make sure that it takes the whole width
    {\@parboxrestore%
        \begin{adjustwidth}{}{\leftmargin}%
    }
    {\end{adjustwidth}%
    }
\makeatother

\newlength\gridskip \setlength\gridskip{6mm plus 1mm minus 1mm} \definecolor{mygray}{gray}{.8} \newcounter{lignes}

\makeatletter \def\fillwithlines#1{% \begin{restoretext} \setcounter{lignes}{1} {% \hrule height \z@ \nobreak \unskip\nobreak\cleaders \hbox to \hsize{% \vrule height \gridskip depth \z@ width \z@ \textcolor{mygray}{\uline{\hfill}\arabic{lignes}\addtocounter{lignes}{1}}} #1\relax } \end{restoretext} } \makeatother

\begin{document}

Find the derivative of $x^2$. \fillwithlines{\vfill}

\end{document}

Here is the result from the example:

As we can see, the line numbers don't increment and simply stay at one. All the rest is perfect.

As a bonus question, if someone can make the gridskip actually use the glue properties independently for each command use in order to perfectly fit the lines into the page, that would be awesome :)

  • Please make your code so it will compile. – cfr Mar 23 '24 at 19:58
  • 1
    Of course, give me a minute though – Alexandre Simard Mar 23 '24 at 19:59
  • Did you base this on ref: https://tex.stackexchange.com/a/241139/ by any chance? If so, please link there or https://tex.stackexchange.com/q/241137/. – cfr Mar 23 '24 at 20:02
  • 1
    Yes I did, sorry for not including that ! I will add it now. – Alexandre Simard Mar 23 '24 at 20:06
  • 1
    Thank you for the tips ! – Alexandre Simard Mar 23 '24 at 20:08
  • Thanks. Just I found that easily because I have a note of the answer in a package I use based on that answer, but other people might not find it easily and the background might be helpful. I think you need a different approach here because this one works by making a box and then keep repeating that box. But you can't do that if you need the counter to increment .... – cfr Mar 23 '24 at 21:05
  • Yeah I think I'll have no choice but to use Tikz. In this case the numbers were mostly personal and add nothing to the exam, but I'll try to rewrite my function and I'll come back to post an answer when I do. – Alexandre Simard Mar 23 '24 at 23:11

2 Answers2

2

This works for the main part of the question. This is much, much, much slower than the code for filling with plain (unnumbered) rules or dots. Instead of copying a single box containing a rule or line of dots, we construct a coffin incrementally. For the first line, we create a base coffin containing the rule. We then attach a number on the right. This is actually in the margin. For each subsequent line, we first test to see if space remains on the page. If so, we add two further coffins: a copy of the base one with the line and a new one with the next line number.

\documentclass[a4paper]{article}
% ateb: https://tex.stackexchange.com/a/713911/
\usepackage{xcolor}
\usepackage[]{geometry}

\makeatletter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % copied verbatim, excepting format from Joseph Wright's' siunitx.sty under LPPL @ifundefined{ExplLoaderFileDate}{% \RequirePackage{expl3}% }{} % almost verbatim from siunitx.sty @ifl@t@r\ExplLoaderFileDate{2022-02-24}{% }{% \PackageError{fillwith}{Support package expl3 too old} {% You need to update your installation of the bundles 'l3kernel' and 'l3packages'.\MessageBreak Loading~fillwith~will~abort!% }% \endinput }% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\RequirePackage{svn-prov} \ExplSyntaxOn

% BEGIN vars \coffin_new:N \l__fillwith_line_coffin \coffin_new:N \l__fillwith_lines_coffin \coffin_new:N \l__fillwith_tmpa_coffin \dim_new:N \l__fillwith_ht_dim \dim_new:N \l__fillwith_rem_dim \dim_new:N \l__fillwith_line_ht_dim \dim_set:Nn \l__fillwith_line_ht_dim { 0.4pt } \int_new:N \l__fillwith_cnt_int % END vars % BEGIN keys \keys_define:nn { fillwith } { fillwith~ht .tl_set:N = \l__fillwith_ht_tl, fillwith~ht .initial:n = 2, color .meta:n = { colour = #1, }, colour .code:n = { \colorlet{fillwithcolour}{#1} }, colour .initial:n = gray, } % END keys % BEGIN fns \cs_new_protected_nopar:Nn \fillwith_style: {} \cs_new_protected_nopar:Nn \fillwith_coffin_tht:N { \dim_eval:n { \coffin_ht:N #1 + \coffin_dp:N #1 } } % BEGIN fillwith_nos: \cs_new_protected:Nn \fillwith_nos: % rhybudd: ARAF ! SLOW! { \color{fillwithcolour} \vcoffin_set:Nnn \l__fillwith_line_coffin { \linewidth } { \vrule height \l__fillwith_line_ht_dim depth \c_zero_skip width \linewidth \fillwith_style: } \coffin_set_eq:NN \l__fillwith_lines_coffin \l__fillwith_line_coffin \int_incr:N \l__fillwith_cnt_int \vcoffin_set:Nnn \l__fillwith_tmpa_coffin { 1.5em} { \int_to_arabic:n { \l__fillwith_cnt_int } } \coffin_attach:NnnNnnnn \l__fillwith_lines_coffin {b} {r} \l__fillwith_tmpa_coffin {b} {l} {2.5pt} {0pt} \dim_set:Nn \l__fillwith_ht_dim { \fp_to_dim:n { \l__fillwith_ht_tl * \baselineskip } } \dim_set:Nn \l__fillwith_rem_dim { \pagegoal -\footskip - \pagetotal - \l__fillwith_ht_dim } \dim_until_do:nNnn { \fillwith_coffin_tht:N \l__fillwith_lines_coffin } > { \l__fillwith_rem_dim - \l__fillwith_ht_dim } { \coffin_join:NnnNnnnn \l__fillwith_lines_coffin { b } { l } \l__fillwith_line_coffin { t } { l } { 0pt } { -\l__fillwith_ht_dim } \int_incr:N \l__fillwith_cnt_int \vcoffin_set:Nnn \l__fillwith_tmpa_coffin {1.5em} { \int_to_arabic:n { \l__fillwith_cnt_int } } \coffin_attach:NnnNnnnn \l__fillwith_lines_coffin {b} {r} \l__fillwith_tmpa_coffin {b} {l} {2.5pt} {0pt}
} \coffin_typeset:Nnnnn \l__fillwith_lines_coffin { t } { l } { 0pt } { -\l__fillwith_ht_dim } \normalcolor } % END \fillwith_nos: % END fns \NewDocumentCommand\fillwithset { +m }{ \keys_set:nn { fillwith } { #1 } } % BEGIN \fillwithnolines \NewDocumentCommand \fillwithnolines { O { } } { \group_begin: \keys_set:nn { fillwith } { #1 } \fillwith_nos: \group_end: } % END \fillwithnolines \ExplSyntaxOff \fillwithset{% colour=gray!80, } \begin{document} \parindent=0pt

Find the derivative of $x^2$.

\fillwithnolines

\end{document}

The only two available keys are fillwith ht and colour (or color). The first determines the factor by which the lines are spread (2 by default). The second determines the colour of the rule and numbers (gray by default).

The rules are always 0.4pt high and \linewidth wide, though you could easily make these dimensions configurable, along with an option to fill some dimension other than the remaining vertical space.

Apologies in advance for the crappy image. KDE/Okular has a new bug which renders bitmap images scrappily. For instance, the PDF is not missing rules randomly! Nor are letters and numbers blotchy and blurry!

doubtless blurry output

For the bonus, there are three possibilities: I'm not sure I understand it, if I do understand it, I'm not sure it is possible and, if it is possible, I don't know how.

cfr
  • 198,882
0

Here is a function written in Tikz that does the same thing that my old function did. It also lets me do math for what I want to print (which is perfect for more complex stuff). Also, I added a little test that adds an additional line when needed (when there is space for 90% of an extra line or more).

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{ulem}
\usepackage{showframe}
\usepackage{changepage}
\usepackage{tikz}

\usepackage{changepage} \makeatletter \newenvironment{restoretext} {@parboxrestore\begin{adjustwidth}{}{\leftmargin}} {\end{adjustwidth}} \makeatother

\newlength\gridskip \setlength\gridskip{7mm} \definecolor{mygray}{gray}{.8}

\usetikzlibrary{calc} \newdimen\topx \newdimen\topy \newdimen\bottomx \newdimen\bottomy

\newcommand{\mylines}[1]{% #1: size of lines area \begin{restoretext} \par \tikz[remember picture, anchor=base]{\coordinate (topans);} #1 \tikz[remember picture, anchor=base]{\coordinate (bottomans);}

\begin{tikzpicture}[remember picture, overlay]
    % \draw (topans) -- (bottomans); % for debugging
    \pgfextractx{\topx}{\pgfpointanchor{topans}{center}}%
    \pgfextracty{\topy}{\pgfpointanchor{topans}{center}}%
    \pgfextractx{\bottomx}{\pgfpointanchor{bottomans}{center}}%
    \pgfextracty{\bottomy}{\pgfpointanchor{bottomans}{center}}%
    \pgfmathsetmacro\ansheight{veclen(\topx-\bottomx,\topy-\bottomy)*1pt}%
    \pgfmathsetmacro\numblines{floor(\ansheight / \gridskip)}; % Number of lines
    \pgfmathsetmacro\diff{frac(\ansheight / \gridskip)}
    \pgfmathsetmacro\start{0}
    \pgfmathsetmacro\endLoop{floor(\numblines)}
    \pgfmathparse{(\diff>0.9)?1:0}\ifdim\pgfmathresult pt>0pt
        \pgfmathsetmacro\endLoop{\endLoop+1};
    \fi
    \foreach \i in {\start,...,\endLoop} {
        \pgfmathsetmacro\pos{(\numblines-\i)+\diff}
        \pgfmathsetmacro\noline{int(\i + 1 - \start)};
        \draw [color=mygray] (0,\pos*\gridskip) -- (\linewidth,\pos*\gridskip) node [above left=-2pt, pos=-0.01] (a) {};
    }
\end{tikzpicture}
\end{restoretext}

}