LaTeX问题:如何让\obeylines环境内的脚注不受其影响?
解决
poesia环境中\obeylines导致脚注格式异常的问题(兼容memoir文档类) 问题根源
\obeylines会强制将文本里的每一个换行符转换为\par(段落结束命令),这会让脚注内容里的换行(包括LaTeX内部排版生成的换行)被强制拆成多个段落,出现格式混乱。我们需要让脚注命令临时绕过\obeylines的影响,同时保留poesia环境本身的换行规则。
解决方案:重定义\footnote命令
下面的方案基于memoir文档类原有\footnote实现,通过临时禁用\obeylines修复脚注格式:
基础版(支持普通脚注)
\documentclass{memoir} % 保存原有footnote命令,避免丢失memoir类的扩展功能 \let\oldfootnote\footnote \makeatletter % 重定义footnote命令 \renewcommand{\footnote}[1]{% \oldfootnote{% % 临时禁用\obeylines,恢复正常换行处理 \let\obeylines\relax #1% }% } \makeatother % 定义你的poesia环境 \newenvironment{poesia}{% \obeylines }{% } \begin{document} \begin{poesia} 这是诗歌第一行\footnote{测试脚注:这里的内容会正常排版为一段,不会因\obeylines强制分段。} 这是诗歌第二行 这是诗歌第三行\footnote{长内容脚注:即使内容较长,换行也会按正常段落处理,不会被拆成多个小段落。} \end{poesia} \end{document}
进阶版(支持可选标记脚注)
如果需要使用\footnote[数字]{内容}这类带可选标记的脚注,可使用以下兼容版本:
\documentclass{memoir} \let\oldfootnote\footnote \makeatletter \renewcommand{\footnote}[2][]{% % 判断是否传入可选参数 \ifx\relax#1\relax \oldfootnote{% \let\obeylines\relax #2% }% \else \oldfootnote[#1]{% \let\obeylines\relax #2% }% \fi } \makeatother \newenvironment{poesia}{% \obeylines }{% } \begin{document} \begin{poesia} 普通脚注测试\footnote{这是无标记参数的脚注。} 带标记脚注测试\footnote[5]{这是指定了标记数字的脚注。} \end{poesia} \end{document}
原理说明
- 先保存原
\footnote命令为\oldfootnote,确保不破坏memoir类对脚注的自定义扩展 - 在重定义的
\footnote中,将脚注内容包裹在局部作用域内,临时把\obeylines设为\relax使其失效 - 局部作用域仅影响脚注内容,
poesia环境内的其他文本依然遵循\obeylines的换行规则
替代底层方案
若想更贴近LaTeX内部机制,可使用\@restorecr恢复原始换行处理逻辑(\obeylines本质是修改了\@cr命令):
\makeatletter \renewcommand{\footnote}[1]{% \oldfootnote{% \@restorecr #1% }% } \makeatother
内容的提问来源于stack exchange,提问作者lazzalazza
相关产品推荐
相关产品推荐

