TeX - LaTeX Asked on February 22, 2021
(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}%
@temp@length=ht0 advance@temp@length by dp0%
% calculate the number of lines needed to place the content
setlength{@temp@length}{numexpr@temp@length/1864679relax cm + #1baselineskip}%
setlength{protectboxskip}{0.5@temp@length}%
vspace{protectboxskip}%
vspace{-0.3baselineskip}%
parnoindent%
begin{minipage}[c][baselineskip]{linewidth}%
abovedisplayshortskip=0pt%
belowdisplayshortskip=0pt%
abovedisplayskip=0pt%
belowdisplayskip=0pt%
#2%
end{minipage}%
vspace{protectboxskip}%
vspace{0.3baselineskip}%
}
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}
letMYequationequation
letendMYequationendequation
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
protecteddef${@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?
As have answered here: my answer to another question, it can be done as follows:
% protectbox<optional, space ajust>{optional, height}{content}
newlength{protectboxskip}
NewDocumentCommandprotectbox{D<>{0}om}
{
IfNoValueTF{#2}%
{% Height not given
% measure the height
setbox0=vbox{%
abovedisplayshortskip=0pt%
belowdisplayshortskip=0pt%
abovedisplayskip=0pt%
belowdisplayskip=0pt%
#3}%
@temp@length=ht0 advance@temp@length by dp0%
% assign the height
setlength{@temp@length}{numexpr@temp@length/baselineskiprelaxbaselineskip + #1baselineskip}%
}%
{% Height given
setlength{@temp@length}{#2baselineskip-baselineskip}%
}
setlength{protectboxskip}{0.5@temp@length}%
% space above
vspace{parskip}%
vspace{baselineskip}%
vspace*{protectboxskip}%
vspace*{-0.2baselineskip}%
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.2baselineskip}%
}
% Redefine equation and equation*
letequationequation
letendequationendequation
RenewEnviron{equation}{%
addtocounter{equation}{-1}
protectbox<0>{%
begin{equation}%
BODY%
end{equation}}%
parnoindent%
}
RenewEnviron{equation*}{%
protectbox<0>{%
addtocounter{equation}{-1}
begin{equation}%
st@rredtrue global@eqnswfalse%
BODY%
end{equation}}%
parnoindent%
}
% Redefine [...]
def[#1]{begin{equation*}#1end{equation*}}
This isn't perfect, but it does work. The final result is like this:
Answered by Jinwen on February 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP