更改回车符的行为

更改回车符的行为

定义一个增强verse环境,其中按回车键一次可形成一个\\,按两次可形成一个\par,按三次可形成一个\medskip,按四次及以上可形成一个\bigskip

例如:

\begin{enhanceverse}
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that I love you


The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtedly knowing the love from both

Yet cannot be together




---Anonymous
\end{enhanceverse}

其应该输出类似的外观。

答案1

您需要激活回车符并使用计数器,如下所示。注意%定义中使用的。它们用于在定义中屏蔽回车符。

\documentclass{article}

\makeatletter

\newenvironment{enhanceverse}{%
    \verse
    \futurelet\next\@enhanceverse
}{%
    \endverse
}

\newcounter{enhanceversecr}

\begingroup
\catcode`\^^M=\active%
\gdef\@enhanceverse{%
    \catcode`\^^M=\active%
    \let^^M\@enhanceverse@cr%
}%
\gdef\@enhanceverse@cr{%
    \stepcounter{enhanceversecr}%
    \@ifnextchar^^M{}{%
        \ifcase\c@enhanceversecr%
        \or\\%
        \or\par%
        \or\par\medskip%
        \else\par\bigskip%
        \fi%
        \setcounter{enhanceversecr}{0}%
    }%
}%
\endgroup%

\makeatother

\begin{document}

\begin{enhanceverse}
The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that I love you


The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtedly knowing the love from both

Yet cannot be together




---Anonymous
\end{enhanceverse}


\end{document}

结果

答案2

ConTeXt 提供了一个lines尊重用户换行符的环境(类似于<pre>HTML 中的标签)。FWIW,您可以通过设置来实现所需的行为

\setuplines[inbetween={\null\blank}]

然后使用

\startlines
 ....
\stoplines

这与您想要的并不完全相同,如果您有三个\r,那么您将获得与两个空行相同的空间,依此类推。但是,如果空行上有空格,这也会起作用,依此类推。

相关内容