\marginpar 适用于整个文档

\marginpar 适用于整个文档

我正在尝试执行以下操作:将输入行号(代码的行号)放在整个 pdf 输出的每个段落上。例如,对于代码

\documentclass{book}
\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\begin{document}
\chapter{First chapter}
Text text text text text text text text text.
\section{First section}
Text text text text text text text text text text text text text text   text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\begin{theorem}This is a theorem.
\end{theorem}
Text text text text text text text text text.

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\end{document}

输出必须是

在此处输入图片描述

笔记:\marginpar{\the\inputlineno}我通过对源代码中的每一行执行此操作来完成此示例。)

为此,我尝试使用该\everypar命令。因此,我尝试了以下操作

\documentclass{book}
\usepackage{amsthm}
\everypar={\marginpar{\the\inputlineno}}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\begin{document}
\chapter{First chapter}
Text text text text text text text text text.
\section{First section}
Text text text text text text text text text text text text text text   text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\begin{theorem}This is a theorem.
\end{theorem}
Text text text text text text text text text.

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\end{document}

但什么也不做。另外,我试图把它放在后面\begin{document},因此数字只出现在该行\chapter。通过应用这篇文章中使用的代码为什么 \everypar 不起作用?.我得到了以下

\documentclass{book}
\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\begin{document}
\let\oldep\everypar \newtoks\everypar\oldep{\the\everypar\the\inputlineno}
\chapter{First chapter}
Text text text text text text text text text.
\section{First section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\begin{theorem}This is a theorem.
\end{theorem}
Text text text text text text text text text.

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\end{document}

在此处输入图片描述

这是我想要的开始。我试图把

\let\oldep\everypar \newtoks\everypar\oldep{\the\everypar\marginpar\the\inputlineno}

但发送了一些错误:

! You can't use `\global' after \the.
\@minipagefalse ->\global 
                      \let \if@minipage \iffalse 
l.7 \chapter{First chapter}

I'm forgetting what you said and using zero instead.

提前致谢。

答案1

像这样?

\documentclass{book}
\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}
\begin{document}
\let\oldpar=\par
\def\par{\hfill\the\inputlineno\oldpar}

\chapter{First chapter}
Text text text text text text text text text.
\section{First section}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\begin{theorem}This is a theorem.
\end{theorem}
Text text text text text text text text text.

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.
\end{document}

在此处输入图片描述

相关内容