![如何在编号环境中忽略某些项目?](https://linux22.com/image/252417/%E5%A6%82%E4%BD%95%E5%9C%A8%E7%BC%96%E5%8F%B7%E7%8E%AF%E5%A2%83%E4%B8%AD%E5%BF%BD%E7%95%A5%E6%9F%90%E4%BA%9B%E9%A1%B9%E7%9B%AE%EF%BC%9F.png)
我有一个环境来给其中的段落编号,如果我必须忽略环境中一些不需要编号的段落,例如:我想忽略的编号\lipsum[6-7]
,该怎么做?
环境的完整代码如下:
\documentclass{book}
\usepackage{mparhack} % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum} % for dummy text
% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
\everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}
\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}
\begin{parnumbers}
\lipsum[5-8]
\end{parnumbers}
\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}
\end{document}
答案1
您可以定义一个带星号的parnumbers
环境版本,例如(虽然我的方法可能比较粗鲁)
\newenvironment{parnumbers*}{%
\par%
\everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill]
{}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}
然后
\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}
应该可以完成这个工作。
完整的 MWE:
\documentclass{book}
\usepackage{mparhack} % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum} % for dummy text
% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
\everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}
\newenvironment{parnumbers*}{%
\par%
\everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill]
{}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}
\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}
\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}
\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}
\end{document}
答案2
我对您的代码进行了如下改进:
\newenvironment{parnumbers*}{%to ignore some items
\par
\everypar{\leavevmode\marginpar[\hfill]
{}}%
}{}
所以正如我想要的那样,lipsum 5 的数字是 1,lipsum 8 的数字是 2,lipsum 6-7 被忽略。
最后一个问题是如何通过命令来实现,而不是这样的环境?
完整的 MWE:
\documentclass{book}
\usepackage{mparhack} % get marginpars to always show up on the correct side (need to compile twice)
\usepackage{lipsum} % for dummy text
% change this to get the formatting you want
\newcommand{\parnum}{\bfseries\arabic{parcount}}
\newcounter{parcount}
\newenvironment{parnumbers}{%
\par%
\everypar{\stepcounter{parcount}\leavevmode\marginpar[\hfill\parnum]
{\parnum}}%
\setcounter{parcount}{0}%to restart numbering in every environment
}{}
\newenvironment{parnumbers*}{%
\par%
\everypar{\leavevmode\marginpar[\hfill]
{}}%
}{}
\begin{document}
\begin{parnumbers}
\lipsum[1-4]
\end{parnumbers}
\begin{parnumbers}
\lipsum[5]
\begin{parnumbers*}
\lipsum[6-7]
\end{parnumbers*}
\lipsum[8]
\end{parnumbers}
\begin{parnumbers}
\lipsum[9-10]
\end{parnumbers}
\end{document}