Search code examples
latexpdflatex

How can I prevent blank lines from being counted in line numbers in Latex?


In the poem I have written in blockquote in Latex, I would like to show the line count. However, I do not want blank lines to count as line numbers. For counting line numbers, I use the lineno.sty package in Latex. If I write \resetlinenumber[5] in the empty line so that the next line (the first line of the second paragraph) is counted as the fifth line, then latex counts the first line of the first paragraph as the fifth line, which is not what I want. Is it possible to avoid counting empty lines in line numbers?

Here is the chunk of compilable code:

\documentclass[a4paper,10pt,fleqn]{report}
\usepackage[top = 1in, bottom = 1in, left = 0.8in, right = 0.8in]{geometry}
\usepackage{enumitem}
%answer lettering
\makeatletter
\newcommand*{\actenum}[1]{
    \expandafter\@actenum\csname c@#1\endcsname
}
\newcommand*{\@actenum}[1]{
    \ifcase#1\or (\textbf{A}) \or (\textbf{B}) \or (\textbf{C}) \or (\textbf{D}) \or (\textbf{E}) \else\@ctrerr\fi
}
\AddEnumerateCounter{\actenum}{\@actenum}{B}
\makeatother
\newsavebox{\fminipagebox}
\NewDocumentEnvironment{fminipage}{m O{\fboxsep}}
 {\par\kern#2\noindent\begin{lrbox}{\fminipagebox}
  \begin{minipage}{#1}\ignorespaces}
 {\end{minipage}\end{lrbox}%
  \makebox[#1]{%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
    \fbox{\usebox{\fminipagebox}}%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
  }\par\kern#2
 }
\newenvironment{blockquote}{%
  \par%
  \medskip
  \leftskip=4em\rightskip=2em%
  \noindent\ignorespaces}{%
  \par\medskip}
\usepackage{lineno}
\modulolinenumbers[5]

\begin{document}
\makeatother
\begin{center}
    \begin{fminipage}{6in}
        \begin{blockquote}
            \begin{internallinenumbers}
                \resetlinenumber
                Last night I heard your voice, mother,\\
                The words you sang to me\\
                When I, a little barefoot boy,\\
                Knelt down against your knee.\\
                
                And tears gushed from my heart, mother,\\
                And passed beyond its wall,\\
                But though the fountain reached my throat\\
                The drops refused to fall.\\
                
                'Tis ten years since you died, mother,\\
                Just ten dark years of pain,\\
                And oh, I only wish that I\\
                Could weep just once again.
            \end{internallinenumbers}
        \end{blockquote}
    \end{fminipage}
\end{center}
\end{document}

And here is the output:

enter image description here


Solution

  • You could avoid the problem by not having empty lines at all:

    \documentclass[a4paper,10pt,fleqn]{report}
    \usepackage[top = 1in, bottom = 1in, left = 0.8in, right = 0.8in]{geometry}
    \usepackage{enumitem}
    %answer lettering
    \makeatletter
    \newcommand*{\actenum}[1]{
        \expandafter\@actenum\csname c@#1\endcsname
    }
    \newcommand*{\@actenum}[1]{
        \ifcase#1\or (\textbf{A}) \or (\textbf{B}) \or (\textbf{C}) \or (\textbf{D}) \or (\textbf{E}) \else\@ctrerr\fi
    }
    \AddEnumerateCounter{\actenum}{\@actenum}{B}
    \makeatother
    \newsavebox{\fminipagebox}
    \NewDocumentEnvironment{fminipage}{m O{\fboxsep}}
     {\par\kern#2\noindent\begin{lrbox}{\fminipagebox}
      \begin{minipage}{#1}\ignorespaces}
     {\end{minipage}\end{lrbox}%
      \makebox[#1]{%
        \kern\dimexpr-\fboxsep-\fboxrule\relax
        \fbox{\usebox{\fminipagebox}}%
        \kern\dimexpr-\fboxsep-\fboxrule\relax
      }\par\kern#2
     }
    \newenvironment{blockquote}{%
      \par%
      \medskip
      \leftskip=4em\rightskip=2em%
      \noindent\ignorespaces}{%
      \par\medskip}
    \usepackage{lineno}
    \modulolinenumbers[5]
    
    \begin{document}
    \makeatother
    \begin{center}
        \begin{fminipage}{6in}
            \begin{blockquote}
                \begin{internallinenumbers}
                    \obeylines
                    \resetlinenumber
                    Last night I heard your voice, mother,
                    The words you sang to me
                    When I, a little barefoot boy,
                    Knelt down against your knee.\medskip
                    
                    And tears gushed from my heart, mother,
                    And passed beyond its wall,
                    But though the fountain reached my throat
                    The drops refused to fall.\medskip
                    
                    'Tis ten years since you died, mother,
                    Just ten dark years of pain,
                    And oh, I only wish that I
                    Could weep just once again.
                \end{internallinenumbers}
            \end{blockquote}
        \end{fminipage}
    \end{center}
    \end{document}
    

    enter image description here