获取 \section 的格式和间距

获取 \section 的格式和间距

我正在尝试创建一个\section文本格式略有不同的新命令。虽然我知道如何使用该titlesec包执行此操作,但我想避免手动插入格式和空格。

到目前为止

\titleclass{\customsec}{straight}[\section]
\newcounter{customsec}

\titleformat{\customsec}{\sffamily\normalsize\bfseries}{}{0em}{Custom text \thecustomsec:~}
\titlespacing*{\customsec}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

目前,我已经手动插入了一些字体、大小等值,但我希望这是 格式的精确副本\section。我唯一需要的更改是能够更改部分标题中的文本。是否有任何命令可以为我提取这些内容?

或者正确的方法是仅在我需要自定义部分的文本部分中更新命令,然后恢复它?

答案1

我不知道如何从默认\section命令中提取确切的样式,但它似乎titlesec没有提供这种功能,而且手动或自动执行它会很脆弱。

但是,仅在本地更改标题标签似乎很容易,并且通过使用 TeX 组(即括号的内层),完成后它会自动更改回默认值。

\documentclass{article}
\usepackage{titlesec}

\newcommand\customsec[1]{{%
    \titlelabel{Custom text \thetitle:~}%
    \section{#1}%
}}

\begin{document}
    \section{normal section}
    \customsec{custom section}
    \section{normal section}
    \customsec{custom section}
\end{document}

结果

编辑:在此帖子的旧版本中,显示了两个更复杂的版本,要么手动重置\titlelabel为默认值,要么访问内部 LaTeX 宏来保存和恢复它。

相关内容