排版诗歌

排版诗歌

我需要排版大约 250 首诗,每首诗约有 40 节。其中 99% 的形式如下:

line 1
line 2
line 3
line 4

line 5
line 6
line 7
line 8

...

每首诗的“来源”都是一个 PDF 文件,我用它复制/粘贴到纯文本文件中。

现在的任务是格式化这些诗歌,同时避免过多的标记。我可以这样做:

\begin{verse}
line 1 \\
line 2 \\
line 3 \\
line 4 \\
\end{verse}

\begin{verse}
line 5 \\
line 6 \\
line 7 \\
line 8 \\
\end{verse}

但我更喜欢不在每节诗句周围添加\begin{verse}“和” \end{verse},并且避免在每行末尾使用双反斜杠。我该怎么办?

一些“规则”

  • 四行诗句内不允许分页
  • 我想避免标记,但如果不可能,也可以添加标记
  • 所有异常均手动处理
  • 无需其他特殊格式(四行均左右齐平,无自动换行
  • 如果可行的话,有些诗歌应该多栏
  • 每首诗都有自己的文件,在开始之前有一个\title{...}和一个(不过我可以把它放在其他地方)。\date{...}

答案1

在此处输入图片描述

\documentclass[twocolumn]{article}

\textheight.5\textheight

\begingroup
\makeatletter
\catcode13\active%
\gdef\verseinput#1{%
{%
\interlinepenalty\@M%
\def^^M{\@ifnextchar^^M\par{\ifhmode\break\fi}}%
\rightskip\fill%
\parindent\z@%
\parskip\baselineskip%
\raggedbottom%
\catcode13\active\input{#1}%
\par%
}}%
\endgroup%
\begin{document}

\verseinput{v1.txt}

\end{document}

line 1
line 2
line 3
line 4

line 5
line 6
line 7
line 8

line 9
line 10
line 11
line 12

line a9
line a10
line a11
line a12

line b9
line b10
line b11
line b12

line a9
line a10
line a11
line a12

line b9
line b10
line b11
line b12

line a9
line a10
line a11
line a12

line b9
line b10
line b11
line b12

line a9
line a10
line a11
line a12

line b9
line b10
line b11
line b12

line a9
line a10
line a11
line a12

line b9
line b10
line b11
line b12

答案2

将以下内容添加到您的序言中,您无需输入任何标记:

\documentclass{article}
\makeatletter
\newdimen\allttindent \allttindent=0pt % set this to change the indent

\def\docspecials{\do\ \do\$\do\&%
  \do\#\do\^\do\^^K\do\_\do\^^A\do\%\do\~}

\def\alltt{\trivlist \item[]\if@minipage\else\vskip\parskip\fi
\leftskip\@totalleftmargin  \advance\leftskip\allttindent \rightskip\z@
\parindent\z@\parfillskip\@flushglue\parskip\z@
\@tempswafalse \def\par{\if@tempswa\hbox{}\fi\@tempswatrue\@@par}
\obeylines \tt \catcode``=13 \@noligs
\let\do\@makeother \docspecials
 \frenchspacing\@vobeyspaces}

\let\endalltt=\endtrivlist
\AtBeginDocument{\alltt}
\AtEndDocument{\endalltt}
\makeatother

\begin{document}
There was an old woman of 92
Parlez-vous
There was an old woman of 92
Parlez-vous
There was an old woman of 92
Did a fart and away it flew
Inky pinky parlez-vous

The fart went rolling down the street
Knocked a copper off his feet

The copper got out his rusty pistol
Blew the fart right on to Bristol

The people of Bristol were having a dance
The fart went rolling on to France

The people of France were not at home
The fart went rolling on to Rome
\end{document}

它不支持多列,但可能可以根据诗歌中的一个单词触发多列。告诉我这个单词,也许 egreg 可以为此想出一个宏!序言借用自alltt2

答案3

另一种选择是使用另一个程序将纯文本诗歌转换为verse环境。此方法的优点是您可以使用它自动处理来自不同文件或数据库的诗歌。

为了防止诗句内部出现分页符,以下答案可能是相关的:https://tex.stackexchange.com/a/21985/7092

作为转换的示例,这里有一个使用 JavaScript 执行此任务的简单工具。在这里,我将一节经文视为由单个换行符分隔的任意数量的行。将下面的代码保存为 html 文件以使用它。

诗句

<html>
<head>
<title>Verses!</title>

<script>
function convertVerses() 
{
    var input = document.getElementById('input').value
    var lines = input.split(/\r?\n/);
    var env = "verse"
    var output = "";
    for (var i = 0;i < lines.length;i++)
    {
        if (!lines[i].match(/^[ \t\n]*$/))
        {   
         //   alert(lines[i])
        // no empty line       
            if (lines.length==1 || 
              (i==0 && lines[i+1].match(/^[ \t\n]*$/)) ||
                (i==lines.length && lines[i-1].match(/^[ \t\n]*$/)) ||
                (lines[i-1].match(/^[ \t\n]*$/) && lines[i+1].match(/^[ \t\n]*$/))) {
                // only line of verse
                output += '\\begin\{'+env+'\}\n' + lines[i] + '\n\\end\{'+env+'\}\n\n';   
            } else  if (i==0 || lines[i-1].match(/^[ \t\n]*$/)) { 
                // first line of verse
                output += '\\begin\{'+env+'\}\n' +lines[i] + '\\\\\n';
            } else if (i==lines.length || lines[i+1].match(/^[ \t\n]*$/))  {
                // last line of verse
                output += lines[i] + '\n\\end\{'+env+'\}\n\n';   
            } else {
                // any other line of the verse 
                output += lines[i] + '\\\\\n';
            }  
        }
    }
    document.getElementById('output').value = output;
}

</script>

<style>
textarea {width:700px;height:330px;display:block;margin:10px}
button {width:700px;height:50px;margin:10px}
</style>
</head>

<body>    
<textarea id="input">
p0

p1
p2
p3 

p5
p6

p9
</textarea>
<button onclick="convertVerses()"> Convert!</button>
<textarea id="output">
</textarea>
</body>    
</html>

相关内容