# Lined-paper line spacing, and redefining the math environment

(The description is kind of long, questions are at bottom)

I'm currently using a lined-paper style, thus need all the contents to be strictly sticking to the line.

I've changed the \baselineskip, have removed the vertical space of itemized environment using enumitem package, and have made some other adjustments, so for now pure text and most in-line math look just fine. However, the displayed math always break the correct line spacing. I tried the code below, but this isn't really helping.

\abovedisplayshortskip=0pt%
\belowdisplayshortskip=0pt%
\abovedisplayskip=0pt%
\belowdisplayskip=0pt%


To avoid manually fixing using \vspace{} every time, I write a "\protectbox" macro to produce line spacing of integer multiple of \baselineskip, which looks like this:

\RequirePackage{calc}
\makeatletter
\newlength{\@temp@length}
\newlength{\protectboxskip}
\newcommand{\protectbox}[2][0]{% the first parameter is an offset of length, the second is the content
% measure the height of contents
\setbox0=\vbox{%
\abovedisplayshortskip=0pt%
\belowdisplayshortskip=0pt%
\abovedisplayskip=0pt%
\belowdisplayskip=0pt%
#2}%
% calculate the number of lines needed to place the content
\setlength{\@temp@length}{\numexpr\@temp@length/1864679\relax cm + #1\baselineskip}%
\setlength{\protectboxskip}{0.5\@temp@length}%
\vspace{\protectboxskip}%
\vspace{-0.3\baselineskip}%
\par\noindent%
\begin{minipage}[c][\baselineskip]{\linewidth}%
\abovedisplayshortskip=0pt%
\belowdisplayshortskip=0pt%
\abovedisplayskip=0pt%
\belowdisplayskip=0pt%
#2%
\end{minipage}%
\vspace{\protectboxskip}%
\vspace{0.3\baselineskip}%
}
\makeatother


Now I can use \protectbox to surround the math environment, so that texts following it are well positioned on the line.

However, sometimes there're so many equations that I want this to be applied automatically. Thus I redefined the equation and equation* environment as follows:

\RequirePackage{amsmath}
\RequirePackage{environ}
\let\MYequation\equation
\let\endMYequation\endequation
\RenewEnviron{equation}{%
\protectbox[-1]{%
\begin{MYequation}%
\BODY%
\end{MYequation}}%
}
\makeatletter
\RenewEnviron{equation*}{%
\protectbox[-1]{%
\begin{MYequation}%
\st@rredtrue \global\@eqnswfalse% this is copied from amsmath.sty
\BODY%
\end{MYequation}}%
}
\makeatother


These two environments work just fine. However, use of $...$ will produce an error (the .log file says it's an "emergency stop", but there's no detail). I find out that $...$ is defined by

\DeclareRobustCommand{$}{\begin{equation*}} \DeclareRobustCommand{$}{\end{equation*}}


Thus I think it should behave exactly like the equation*, but it's not.

I also tried to redefine the $$...$$ since in markdown we usually use this for displayed math and I'm also using the markdown package. The code looks like this:

\catcode$=\active \makeatletter \protected\def${\@ifnextchar$\@doubledollar\@singledollar} \def\@doubledollar$#1{$#1$} \def\@singledollar#1{$$#1$$} \makeatother  However the compiler throws an error Package tikz: Sorry, some package has redefined the meaning of the math-mode dollar sign. This is incompatible with tikz and its calc library and might cause unrecoverable errors.  Now finally the questions: 1. (The most important) Is there some cleverer way to solve the lined-paper-style alignment problem? I think my solution for alignment is somewhat ugly, but after a lot of searching, I did not find other solutions. 2. Is there some way to integrate the \protectbox into math environments  ... , $...$, etc?

3. Why does environment* work but $...$ produce errors?

• I've added the tag "grid-typesetting" here because that is the recognized name for the layout you are trying to get. Please add a link to your later question about redefining the math structures; that has been noted as an "XY" question, and adding this information will make it much clearer. Jul 22 '20 at 18:22

As have answered here: my answer to another question, it can be done as follows:

% \protectbox<optional, space ajust>{optional, height}{content}
\newlength{\protectboxskip}
\NewDocumentCommand\protectbox{D<>{0}om}
{
\IfNoValueTF{#2}%
{% Height not given
% measure the height
\setbox0=\vbox{%
\abovedisplayshortskip=0pt%
\belowdisplayshortskip=0pt%
\abovedisplayskip=0pt%
\belowdisplayskip=0pt%
#3}%
% assign the height
\setlength{\@temp@length}{\numexpr\@temp@length/\baselineskip\relax\baselineskip + #1\baselineskip}%
}%
{% Height given
\setlength{\@temp@length}{#2\baselineskip-\baselineskip}%
}
\setlength{\protectboxskip}{0.5\@temp@length}%
% space above
\vspace{\parskip}%
\vspace{\baselineskip}%
\vspace*{\protectboxskip}%
\vspace*{-0.2\baselineskip}%
\vspace{-\parskip}%
\vspace{-\baselineskip}%
\par%
\noindent%
% the content
\begin{minipage}[c][\baselineskip]{\linewidth}%
\abovedisplayshortskip=0pt%
\belowdisplayshortskip=0pt%
\abovedisplayskip=0pt%
\belowdisplayskip=0pt%
#3%
\end{minipage}%
% space after
\vspace{\protectboxskip}%
\vspace{0.2\baselineskip}%
}

% Redefine equation and equation*
\let\equation\equation
\let\endequation\endequation
\RenewEnviron{equation}{%
\protectbox<0>{%
$$% \BODY%$$}%
\par\noindent%
}
\RenewEnviron{equation*}{%
\protectbox<0>{%
\st@rredtrue \global\@eqnswfalse$$% \BODY%$$}%
% Redefine $...$
\def$#1${\begin{equation*}#1\end{equation*}}
`