关于利用lineno宏包实现“笔记本段落下划线”的一个问题

发布于 2022-04-14 16:22:52

各位大佬,我现有如下代码,利用lineno宏包实现“笔记本段落下划线”效果

\documentclass{ctexart}
\usepackage{mathtools}
\usepackage[mathlines,displaymath]{lineno}
\usepackage{tikz}
\renewcommand\makeLineNumber
{%\tikz[remember picture,overlay,baseline=0.3em]
 % \draw[red!20] (0,0em) -- (\textwidth,0em);
\mbox{\rule[-0.4em]{\textwidth}{0.2pt}}
}
\usepackage{zhlipsum}
\AtBeginDocument{
\lineskiplimit=4pt
\lineskip=5pt
}

\usepackage{etoolbox}        
%% Patch 'normal' math environments:
\newcommand*\linenomathpatch[1]{%
  \cspreto{#1}{\linenomath}%
  \cspreto{#1*}{\linenomath}%
  \csappto{end#1}{\endlinenomath}%
  \csappto{end#1*}{\endlinenomath}%
}
%% Patch AMS math environments:
\newcommand*\linenomathpatchAMS[1]{%
  \cspreto{#1}{\linenomathAMS}%
  \cspreto{#1*}{\linenomathAMS}%
  \csappto{end#1}{\endlinenomath}%
  \csappto{end#1*}{\endlinenomath}%
}

%% Definition of \linenomathAMS depends on whether the mathlines option is provided
\expandafter\ifx\linenomath\linenomathWithnumbers
  \let\linenomathAMS\linenomathWithnumbers
  %% The following line gets rid of an extra line numbers at the bottom:
  \patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
\else
  \let\linenomathAMS\linenomathNonumbers
\fi

\linenomathpatch{equation}
\linenomathpatchAMS{gather}
\linenomathpatchAMS{multline}
\linenomathpatchAMS{align}
\linenomathpatchAMS{alignat}
\linenomathpatchAMS{flalign}

% Disable line numbering during measurement step of multline
\makeatletter
\patchcmd{\mmeasure@}{\measuring@true}{
  \measuring@true
  \ifnum-\linenopenaltypar>\interdisplaylinepenalty
    \advance\interdisplaylinepenalty-\linenopenalty
  \fi
  }{}{}
\makeatother
\begin{document}
\linenumbers
文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文$\dfrac{1}{2}$文字文字文
文字文字文字文字文字文字文字文字文字文字文文
%\begin{linenomath}
\begin{align}
a+b\\
\int c+d
\end{align}
%\end{linenomath}
字文字文字文字文字文字文字文字文字
$\begin{dcases}
  a+b\\ f+r\\ go
\end{dcases}$
\begin{enumerate}
  \item 测试测试测试测试测试测试测试测试$\displaystyle\int$
  \item 测试测试测试测试测试测试测试测试
\end{enumerate}
行间公式块
\[
\begin{dcases}
x\\
-x
\end{dcases}
\]
文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本
%\nolinenumbers
\end{document}

2db64cb3a784031649015041716e1e4.jpg
我的问题是,数学公式之类的会引起行距变化的情况下,如何改进代码,让下划线能够真好位于底部,而不是穿越公式文本。

查看更多

关注者
0
被浏览
766
雾月
雾月 2022-04-14
这家伙很懒,什么也没写!

不难实现,只需:

\renewcommand*\@LN@depthbox{%
  \dp\@tempboxa=\@tempdima
  \nointerlineskip \box\@tempboxa \kern-\@tempdima}

实际上原来的这个命令正是为了行号能对齐基线的。

为了显示效果,你可能还需要改改 \rule 的移动距离和 \lineskip

\renewcommand\makeLineNumber
  {%\tikz[remember picture,overlay,baseline=0.3em]
   % \draw[red!20] (0,0em) -- (\textwidth,0em);
    \mbox{\rule[-0.2em]{\textwidth}{0.2pt}}
  }
\AtBeginDocument{%
  \lineskiplimit=4pt
  \lineskip=8pt
}

image.png

完整代码:

\documentclass{ctexart}
\usepackage{mathtools}
\usepackage[mathlines,displaymath]{lineno}
\usepackage{tikz}

\usepackage{zhlipsum}

\usepackage{etoolbox}        
%% Patch 'normal' math environments:
\newcommand*\linenomathpatch[1]{%
  \cspreto{#1}{\linenomath}%
  \cspreto{#1*}{\linenomath}%
  \csappto{end#1}{\endlinenomath}%
  \csappto{end#1*}{\endlinenomath}%
}
%% Patch AMS math environments:
\newcommand*\linenomathpatchAMS[1]{%
  \cspreto{#1}{\linenomathAMS}%
  \cspreto{#1*}{\linenomathAMS}%
  \csappto{end#1}{\endlinenomath}%
  \csappto{end#1*}{\endlinenomath}%
}

%% Definition of \linenomathAMS depends on whether the mathlines option is provided
\expandafter\ifx\linenomath\linenomathWithnumbers
  \let\linenomathAMS\linenomathWithnumbers
  %% The following line gets rid of an extra line numbers at the bottom:
  \patchcmd\linenomathAMS{\advance\postdisplaypenalty\linenopenalty}{}{}{}
\else
  \let\linenomathAMS\linenomathNonumbers
\fi

\linenomathpatch{equation}
\linenomathpatchAMS{gather}
\linenomathpatchAMS{multline}
\linenomathpatchAMS{align}
\linenomathpatchAMS{alignat}
\linenomathpatchAMS{flalign}

% Disable line numbering during measurement step of multline
\makeatletter
\patchcmd{\mmeasure@}{\measuring@true}{
  \measuring@true
  \ifnum-\linenopenaltypar>\interdisplaylinepenalty
    \advance\interdisplaylinepenalty-\linenopenalty
  \fi
  }{}{}
\def\@LN@depthbox{%
  \dp\@tempboxa=\@tempdima
  \nointerlineskip \box\@tempboxa \kern-\@tempdima}
\renewcommand\makeLineNumber
  {%\tikz[remember picture,overlay,baseline=0.3em]
   % \draw[red!20] (0,0em) -- (\textwidth,0em);
    \mbox{\rule[-0.2em]{\textwidth}{0.2pt}}
  }
\AtBeginDocument{
\lineskiplimit=4pt
\lineskip=8pt
}
\makeatother
\begin{document}

\linenumbers
文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字
文字文字文字文字文字文字文字文$\dfrac{1}{2}$文字文字文
文字文字文字文字文字文字文字文字文字文字文文
%\begin{linenomath}
\begin{align}
a+b\\
\int c+d
\end{align}
%\end{linenomath}
字文字文字文字文字文字文字文字文字
$\begin{dcases}
  a+b\\ f+r\\ go
\end{dcases}$
\begin{enumerate}
  \item 测试测试测试测试测试测试测试测试$\displaystyle\int$
  \item 测试测试测试测试测试测试测试测试
\end{enumerate}
行间公式块
\[
\begin{dcases}
x\\
-x
\end{dcases}
\]
文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本
%\nolinenumbers
\end{document}

通过修改 \@LN@depthbox 中的 \kern,你可以将行号放置于任意处。
比如:

\def\@LN@depthbox{%
  \dp\@tempboxa=\@tempdima
  \nointerlineskip \kern-.3\@tempdima \box\@tempboxa \kern-.7\@tempdima}
1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览