重新定义 `\par` 命令

重新定义 `\par` 命令

我正在写一本拉丁文诗集(摘自恩尼乌斯的年鉴)。

我定义了一些命令和一个环境来对它们进行编号,但我无法自动将最后一节居中。我知道重新定义命令\par并不是一个好主意,问题肯定出在这里。

这是MWE:

\documentclass[a4paper, 12pt, latin]{minimal}
\usepackage[latin]{babel}

% Convert arabic number to roman number
\newcommand{\Romannum}[1]{%
    \MakeUppercase{%
        \ifnum#1<1
        \ifnum#1=0
        o%
        \else
            -\romannumeral -#1%
        \fi
        \else
        \romannumeral #1%
        \fi}}

% Fragment number
\newcounter{frammento}
\newcommand{\framm}{%
    \stepcounter{frammento}%
    \centering\Romannum\theframmento
    }

% Environment for fragments
\newenvironment{frammpar}{%
    \smallskip
    \def\par{%
        \vspace{1em}%
        \pdfprimitive\par\framm%
        \pdfprimitive\par\centering%
        }%

    }%
    {%
        \newline%
    }

\begin{document}
        \begin{frammpar}
        Musae quae pedibus magnum pulsatis Olympum

        Musas quas memorant, nosce nos esse \textit{Camenas}

        Nam latos populos res atque po\"emata nostra cluebant

        somno leni placidoque revinctus
    \end{frammpar}
\end{document}

然后结果是: 編輯結果。

问题是什么?解决该问题的最佳方法是什么?

答案1

我会避免\par完全重新定义:

在此处输入图片描述

\documentclass[a4paper, 12pt, latin]{article}
\usepackage[latin]{babel}


% Fragment number
\newcounter{frammento}
\newcommand{\framm}{%
    \par
    \refstepcounter{frammento}%
    \addvspace{1em}%
    \centering
    {\everypar{}\Roman{frammento}\par\noindent}
    }

% Environment for fragments
\newenvironment{frammpar}{%
    \par
    \smallskip
    \centering
    \everypar{\framm}%
    }%
    {%
     \par
    }

\begin{document}


        \begin{frammpar}

        Musae quae pedibus magnum pulsatis Olympum


% note multiple blank lines



        Musas quas memorant, nosce nos esse \textit{Camenas}

        Nam latos populos res atque po\"emata nostra cluebant

        somno leni placidoque revinctus
    \end{frammpar}
\end{document}

答案2

最后你需要用 a\pdfprimitive\par来代替 the 。\newline

\documentclass[a4paper, 12pt, latin]{minimal}
\usepackage[latin]{babel}

% Convert arabic number to roman number
\newcommand{\Romannum}[1]{%
    \MakeUppercase{%
        \ifnum#1<1
        \ifnum#1=0
        o%
        \else
            -\romannumeral -#1%
        \fi
        \else
        \romannumeral #1%
        \fi}}

% Fragment number
\newcounter{frammento}
\newcommand{\framm}{%
    \stepcounter{frammento}%
    \centering\Romannum\theframmento
    }

% Environment for fragments
\newenvironment{frammpar}{%
    \smallskip
    \def\par{%
        \vspace{1em}%
        \pdfprimitive\par\framm%
        \pdfprimitive\par\centering%
        }%

    }%
    {%
        %\newline%
        \pdfprimitive\par
    }

\begin{document}
        \begin{frammpar}
        Musae quae pedibus magnum pulsatis Olympum

        Musas quas memorant, nosce nos esse \textit{Camenas}

        Nam latos populos res atque po\"emata nostra cluebant

        somno leni placidoque revinctus
    \end{frammpar}
\end{document}

在此处输入图片描述

答案3

\par在调用时保存原始定义frammpar,然后使用它来关闭环境。

\documentclass[a4paper, 12pt, latin]{minimal}
\usepackage[latin]{babel}

% Convert arabic number to roman number
\newcommand{\Romannum}[1]{%
    \MakeUppercase{%
        \ifnum#1<1
        \ifnum#1=0
        o%
        \else
            -\romannumeral -#1%
        \fi
        \else
        \romannumeral #1%
        \fi}}

% Fragment number
\newcounter{frammento}
\newcommand{\framm}{%
    \stepcounter{frammento}%
    \centering\Romannum\theframmento
    }

% Environment for fragments
\newenvironment{frammpar}{%
    \smallskip
    \let\oldpar\par%
    \def\par{%
        \vspace{1em}%
        \pdfprimitive\par\framm%
        \pdfprimitive\par\centering%
        }%

    }%
    {%
        \oldpar%
    }

\begin{document}
        \begin{frammpar}
        Musae quae pedibus magnum pulsatis Olympum

        Musas quas memorant, nosce nos esse \textit{Camenas}

        Nam latos populos res atque po\"emata nostra cluebant

        somno leni placidoque revinctus
    \end{frammpar}
\end{document}

在此处输入图片描述

相关内容