测试在 {verse} 环境之后起作用的段落开头

测试在 {verse} 环境之后起作用的段落开头

我一直在使用一个命令来自动放置一个数字(如果位于新段落的开头,则特别放置在基线上),否则通常放置在上标上。请参阅\fooauto下面的列表。它用于ifvmode测试段落的开头。代码由此处的问答提供帮助:宏如何检测其是否位于段落开头

不幸的是,ifvmode对于文本遵循环境的情况,这还不够充分{verse}ifvmode在这种情况下,无论文本是否开始一个新段落,测试都会返回 true。

我怎样才能使段落开头测试更好地处理这种情况?

(请注意,对环境的修改{verse}对于显示此问题来说不是必要的,但可以说明我试图模仿的牛津出版社使用的无间隙外观。)

\documentclass{article}
\makeatletter
% Copied from article.cls: \topsep and \partopsep added:
\renewenvironment{verse}
               {\let\\\@centercr
                \list{}{\itemsep      \z@
                        \topsep       \z@% new
                        \partopsep    0.5\baselineskip% new
                        \itemindent   -1.5em%
                        \listparindent\itemindent
                        \rightmargin  \leftmargin
                        \advance\leftmargin 1.5em}%
                \item\relax}
               {\endlist}

\newcommand*\footxt[1]{\textsuperscript{#1}} % used if not at start of paragraph
\newcommand*\foopar[1]{#1\enspace} % used if atstart of paragraph
% See https://tex.stackexchange.com/questions/86143/
\newcommand*\fooauto[1]{%
  \ifvmode% Current test for beginning of paragraph, fails after {verse} env
    \foopar{#1}%
    \expandafter\@gobble%
  \else%
    \expandafter\@firstofone
  \fi%
  {\footxt{#1}}}
\makeatother
\begin{document}
\fooauto{1}The fooauto{} command works correctly here at the
beginning of a new paragraph.
\fooauto{2}The fooauto{} command works correctly here when not at the
beginning of a new paragraph.
\begin{verse}
    Start of verse\\
    End of verse.
\end{verse}

\fooauto{3}Start of new paragraph, ifvmode test works correctly.
\begin{verse}
    Start of verse\\
    End of verse.
\end{verse}
\fooauto{4}Not the start of a new paragraph, ifvmode test in fooauto{} failed.
(But Latex somehow knew not to indent, so it knows that we're not
at the beginning of a paragraph.)
\end{document}

输出

答案1

使用提供的 MWE 进行更新

这使用与下面相同的逻辑,但只是修改了您的fooauto宏:

\newif\ifusefoopar
\newcommand*\fooauto[1]{%
  \if@endpe
    \usefooparfalse
  \else
    \usefoopartrue
  \fi
  \ifvmode
  \else
    \usefooparfalse
  \fi
  \ifusefoopar
    \foopar{#1}%
    \expandafter\@gobble%
  \else%
    \expandafter\@firstofone
  \fi%
  {\footxt{#1}}}

旧答案

虽然不是明确的,但看起来有点像你试图排版圣经的一部分,因为有些圣经以这种方式印刷经文。

我计划将这种功能纳入我的scripture包,但尚未这样做。

但是,根据您的要求的复杂程度,现在可能已经可以充分地对其进行破解。

在以下解决方案中,我没有尝试使段落的开头之内诗歌部分。我也不是 100% 确定这个@endpe测试,但它似乎有效。

\documentclass{article}
\usepackage{scripture}
\scripturesetup{
  poetry/indent=0pt
}
\usepackage{xpatch}
\ExplSyntaxOn
\bool_new:N \g__dedded_at_para_start_bool
\xpretocmd { \__scripture_verse_output:n }
  {
    \legacy_if:nTF { @endpe }
      { \bool_gset_false:N \g__dedded_at_para_start_bool }
      { \bool_gset_true:N \g__dedded_at_para_start_bool }
    \mode_if_vertical:F
      { \bool_gset_false:N \g__dedded_at_para_start_bool }
  }
  { }
  { }
\AddToHook { scripture/verse/before }
  {
    \bool_if:NF \l__scripture_active_inner_bool
      {
        \bool_if:NT \g__dedded_at_para_start_bool
          {
            \cs_set:Nn \__scripture_verse_format:n { \textbf{ #1 } \enspace }
          }
      }
    \bool_gset_false:N \g__dedded_at_para_start_bool
  }
\ExplSyntaxOff
\begin{document}
\begin{scripture}
  \vs{1}This is the start of a paragraph. \vs{2}This is not the start of a
  paragraph. \vs{3}This is not the start of a paragraph.
  \begin{poetry}
    Start of verse
    End of verse.
  \end{poetry}

  \vs{4}This is the start of a paragraph. \vs{5}This is not the start of a
  paragraph. \vs{6}This is not the start of a paragraph.
  \begin{poetry}
    Start of verse
    End of verse.
  \end{poetry}
  \vs{7}This is not the start of a paragraph. \vs{8}This is not the start of a
  paragraph. \vs{9}This is not the start of a paragraph.

  This is the start of a paragraph. \vs{10}This is not the start of a
  paragraph. \vs{11}This is not the start of a paragraph.
\end{scripture}
\end{document}

输出

相关内容