BITS结构XML转TeX时boxed-text内章节标题层级错误解决咨询
解决方案
问题原因
现有代码通过统计全局sec祖先节点的数量判断标题层级,没有排除<boxed-text>容器的影响,因此容器内部的章节会继承外部的层级计数,导致层级错位。
修改思路
- 新增
<boxed-text>标签的匹配模板,负责输出LaTeX的annotebox环境包裹符 - 计算标题层级时增加分支判断:如果当前标题位于
<boxed-text>内部,仅统计从当前sec到上层最近的<boxed-text>之间的sec祖先数量,不从全局根节点开始统计 - 保持
<boxed-text>外的原层级计算逻辑不变
修改后完整XSL代码
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:functx="http://www.functx.com" xmlns:xhtml="http://www.w3.org/1999/xhtml" version="2.0" > <xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-8" use-character-maps="latex"/> <!-- 新增boxed-text匹配模板,输出annotebox环境 --> <xsl:template match="boxed-text[@content-type='annotation']"> <xsl:text>\begin{annotebox} </xsl:text> <xsl:apply-templates/> <xsl:text>\end{annotebox} </xsl:text> </xsl:template> <xsl:template match="title"> <xsl:choose> <xsl:when test="parent::sec"><!--Section Head--> <!-- 计算当前标题层级,区分box内外 --> <xsl:variable name="in-box" select="exists(ancestor::boxed-text)"/> <xsl:variable name="level" select="if ($in-box) then count(ancestor::sec[ancestor::boxed-text]) + 1 else count(ancestor::sec)"/> <xsl:variable name="has-label" select="parent::sec/label!=''"/> <!-- 输出编号定义 --> <xsl:if test="$has-label"> <xsl:value-of select=" if($level = 6) then '\def\thesubparagraph{' else if($level = 5) then '\def\theparagraph{' else if($level = 4) then '\def\thesubsubsection{' else if($level = 3) then '\def\thesubsection{' else if($level = 2) then '\def\thesection{' else if($level = 1) then '\def\thechapter{' else ''" disable-output-escaping="yes"/> <xsl:value-of select="parent::sec/label"/> <xsl:text disable-output-escaping="yes">} </xsl:text> </xsl:if> <!-- 输出标题命令 --> <xsl:value-of select=" if($level = 6) then (if($has-label) then '\subparagraph{' else '\subparagraph*{') else if($level = 5) then (if($has-label) then '\paragraph{' else '\paragraph*{') else if($level = 4) then (if($has-label) then '\subsubsection{' else '\subsubsection*{') else if($level = 3) then (if($has-label) then '\subsection{' else '\subsection*{') else if($level = 2) then (if($has-label) then '\section{' else '\section*{') else ''" disable-output-escaping="yes"/> <xsl:apply-templates/> <xsl:text>} </xsl:text> </xsl:when> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
效果说明
修改后代码会自动识别<boxed-text>边界:
- 容器外章节仍然按全局层级生成对应LaTeX标题命令,符合原业务逻辑
- 容器内部第一节从
\section开始,子层级依次对应\subsection/\subsubsection/\paragraph,和你给出的预期输出完全一致 - 编号规则保持不变,有label的标题输出带编号命令,无label的输出带星号的无编号命令
内容的提问来源于stack exchange,提问作者rcv
相关产品推荐
相关产品推荐

