排版包含散文的诗歌的最聪明的方法是什么?

排版包含散文的诗歌的最聪明的方法是什么?

我有一个脚本,用于查询包含诗歌的数据库并在LaTeX环境中排版它们verse。有些诗歌包含散文部分。这些散文行在数据库中有明确的标记(<prose> ... </prose>),因此我可以对其进行操作。

然后,我正在探索如何巧妙地排版它们LaTeX(也许定义一个特定的在诗句中“子”环境?)。

结果应该是

  1. 诗歌行数的计数在散文行处停止,然后立即继续;
  2. 我应该能够定义在诗句中就像我对诗歌环境所做的那样,对整个文档进行全局散文。

请考虑以下 MWE,其verse环境由我的脚本排版。

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{verse}

\begin{document}

\verselinenumbersleft
\setlength{\vrightskip}{-2em}
\poemlines{1}

\begin{verse}
To see a World in a Grain of Sand\\
And a Heaven in a Wild Flower,\\!
<prose>AND here some prose, which shouldn't be enumerated and also should be styled differently</prose>\\!
Hold Infinity in the palm of your hand\\
And Eternity in an hour.\\!
\end{verse}

\end{document}

产生

在此处输入图片描述

答案1

使用环境insideverse,正如您自己建议的那样,禁用内部的行号,存储行计数器编号并在散文部分结束后将其存储回去。散文部分的样式可以在begin环境的命令部分中更改。外部格式不受影响,因为一切都是安全的\begingroup...\endgroup

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{verse}
\usepackage{xcolor}

\newcounter{saveverselinecounter}

\newenvironment{insideverse}{%
\setcounter{saveverselinecounter}{\number\value{poemline}}%
\let\thepoemline\relax%
% Style commands % Change at will
\bfseries\color{red}%
}{%
\setcounter{poemline}{\number\value{saveverselinecounter}}
}

\begin{document}

\verselinenumbersleft
\setlength{\vrightskip}{-2em}
\poemlines{1}

\begin{verse}
To see a World in a Grain of Sand\\
And a Heaven in a Wild Flower,\\!
\begin{insideverse}%
<prose>AND here some prose, which shouldn't be enumerated and also should be styled differently</prose>\\!
\end{insideverse}%
Hold Infinity in the palm of your hand\\
And Eternity in an hour.\\!
\end{verse}

\end{document}

在此处输入图片描述

编辑:改进版本,对insideverse环境进行一些控制

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{verse}
\usepackage{xcolor}

\usepackage{etoolbox}
\usepackage{xkeyval}


\makeatletter
\newcounter{saveverselinecounter}[verse@envctr]
\makeatother


\providecommand{\ProvideBool}[2][false]{%
\providebool{#2}%
\setbool{#2}{#1}
\long\csgdef{Enable#2}{%
\booltrue{#2}%
}%
\long\csgdef{Disable#2}{%
\boolfalse{#2}%
}%
}%

\ProvideBool{InsideVerseCentered}%

\makeatletter
\define@boolkey[KV]{InsideVerseKeys}{center}[false]{%
\ifKV@InsideVerseKeys@center%
\EnableInsideVerseCentered%
\else%
\typeout{Nope, disabled!}
\DisableInsideVerseCentered%
\fi%
}%

\define@key{InsideVerseKeys}{topskip}[\baselineskip]{%
\def\KVInsideVerseKeysTopSkip{#1}%
}%

\define@key{InsideVerseKeys}{bottomskip}[\baselineskip]{%
\def\KVInsideVerseKeysBottomSkip{#1}%
}%

\makeatletter



\newenvironment{insideverse}[1][]{%
\setkeys{InsideVerseKeys}{#1}%
\setcounter{saveverselinecounter}{\number\value{poemline}}%
\let\thepoemline\relax%
% Style commands % Change at will
\vspace{\KVInsideVerseKeysTopSkip}%
\bfseries\color{red}%
\ifbool{InsideVerseCentered}{%
\centering%
}{%
\relax%
}%
}{%

\setcounter{poemline}{\number\value{saveverselinecounter}}%
\DisableInsideVerseCentered%
\vspace{\KVInsideVerseKeysBottomSkip}%
}%



\AtBeginDocument{%
\EnableInsideVerseCentered%
}%

\presetkeys{InsideVerseKeys}{center=false,%
                             topskip=0.1\baselineskip,%
                             bottomskip=0.1\baselineskip%
                           }{}%

\begin{document}

\verselinenumbersleft
\setlength{\vrightskip}{-2em}
\poemlines{1}

\begin{verse}
To see a World in a Grain of Sand\\
And a Heaven in a Wild Flower,\\!
\begin{insideverse}[center=true]%
AND here some prose, which shouldn't be enumerated and also should be styled differently%</prose>
\end{insideverse}%
Hold Infinity in the palm of your hand\\
And Eternity in an hour.\\!
\end{verse}

\begin{verse}[1em]
To see a World in a Grain of Sand\\
And a Heaven in a Wild Flower,\\!
\begin{insideverse}[center=false]%
AND here some prose, which shouldn't be enumerated and also should be styled differently%</prose>
\end{insideverse}%
Hold Infinity in the palm of your hand\\
And Eternity in an hour.\\!
\end{verse}


\end{document}

在此处输入图片描述

相关内容