使用 ConTeXt 的 \startlines 预格式化文本

使用 ConTeXt 的 \startlines 预格式化文本

这是

我正在使用 pdf 文本提取的结果来生成源文件,以便从其 pdf 中重现(公共领域)书籍,同时尝试保留尽可能多的原始布局,尤其是换行符。到目前为止,Latex 在这方面表现良好,但我正在研究 ConTeXt 作为替代方案,部分是出于好奇。

到目前为止,我已经手动插入\linebreak(在 LaTeX 中)或\break(在 ConTeXt 中)以强制换行算法遵守原始换行符,但阅读我遇到的文档后,我\startlines 发现我不必在文本中乱放这么多垃圾。

文档将中间论证描述为:

command that is expanded and then placed between each line and the next

但以下 MWE 的行为并不如预期:

\starttext
\setuplines[inbetween=\break]

\startlines
First line --- should be fully justified
Secondline --- should be fully justified
\stoplines

\stoptext

事实上,用一些标记测试代替中间参数表明inbetween代码只插入在段落休息:

\starttext
\setuplines[inbetween=FOO]

\startlines
First line

Secondline
\stoplines

\stoptext

另外一个值得关注的问题是,内容可能包含多个段落,因此空白行(现在附加有\break)不应强制生成新页面。

编辑1:理想情况下我应该能够使用类似的东西:

\setupindenting[yes,0.1in]
\startX
first line of first paragraph, whose end becomes a tex linebreak
second line of first paragraph, whose end becomes a tex linebreak
third line, left unjustified.

% parbreak
first line of second paragraph...
\stopX

与 HTML 标签的工作方式类似<pre>

答案1

正如 Metafox 所解释的回答回到你之前的问题,你可以使用paragraph对齐来实现这个效果。例如,

\showframe % to visualize the page
\starttext
\setuplines[align=paragraph]

\startlines
First line --- should be fully justified
Secondline --- should be fully justified
\stoplines

\stoptext

给出

在此处输入图片描述

编辑\startlines这是获得服从的一种方法\parindent(相当假\parindent)。

\setuppapersize[A5]

\definemeasure[indent][20pt]

\setupindenting[\measure{indent},yes]

\define\ResetIndent
    {\let\Indent\FirstIndent}

\define\FirstIndent
    {\hskip\measure{indent}%
     \let\Indent\relax}

\let\Indent\FirstIndent

\setuplines
  [
    align=paragraph, 
    command={\Indent},
    inbetween={\blank\ResetIndent},
    option=packed,
  ]


\starttext
\input knuth

\startlines
Thus, I came to the conclusion that the designer of a 
new system must not only be the implementer and first 
large--scale user; the designer should also write the first 
user manual. \crlf

The separation of any of these four components would
have hurt TEX significantly. If I had not participated fully
in all these activities, literally hundreds of improvements
would never have been made, because I would never have
thought of them or perceived why they were important. \crlf

But a system cannot be successful if it is too strongly
influenced by a single person. Once the initial design is
complete and fairly robust, the real test begins as people
with many different viewpoints undertake their own exper-
iments. \crlf
\stoplines

\stoptext

一些评论:

  • 我使用\definemeasure\measure{..}在行环境中设置缩进和使用缩进时使用相同的值。

  • option=packed确保环境内没有分页符lines。因此,您必须在单独的行环境中添加每一页。

  • 我已经手动添加到了\crlf每段的最后一行。如果您不想手动添加\crlf到每段的末尾,那么一个选项是捕获环境的内容作为缓冲区,并通过算法添加到\crlf每段的末尾。例如,参见这个例子在 wiki 中。

答案2

关注@Aditya LuaTeX参考,这里给出一个基于lua的替代解决方案:

\setuppapersize[A5]
\setupindenting[0.2in,yes]

% Create an environment that stores everything 
% between \startpreformatted and \stoppreformatted 
% in a buffer named 'preformatted'.
\def\startpreformatted
{\dostartbuffer
    [preformatted]
    [startpreformatted]
    [stoppreformatted]}

% On closing the preformatted environment, call the LuaTeX
% function preformatted(), and pass it the contents of 
% the buffer called 'preformatted'
\def\stoppreformatted
{\ctxlua
    {userdata.preformatted(buffers.getcontent('preformatted'))}}

\startluacode
userdata = userdata or {}

function userdata.isemptyline(line)
    return nil ~= string.find(line,'^ *$' )
end

function userdata.preformatted(content)
    local lines    = string.splitlines(content)
    for i=1,#lines do
        if i < #lines then
            if (not userdata.isemptyline(lines[i+1])) and (not userdata.isemptyline(lines[i])) then 
                lines[i] = lines[i]  .. "\\break"    
            end     
        -- else do something special for last line
        end  
    end

    content = table.concat(lines,'\n') 
    context(content) 
end
\stopluacode

\starttext
\startpreformatted 
Thus, I came to the conclusion that the designer of a 
new system must not only be the implementer and first 
large--scale user; the designer should also write the first 
user manual.  

The separation of any of these four components would
have hurt TEX significantly. If I had not participated fully
in all these activities, literally hundreds of improvements
would never have been made, because I would never have
thought of them or perceived why they were important. 
\stoppreformatted  

\stoptext

结果:

在此处输入图片描述

相关内容