2

I'm trying to create a new \section command with a slightly different text formatting. While I can figure out how to do this with the titlesec package, I would like to avoid manually inserting the formatting and spacing.

So far I have

\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}

Currently I have manually inserted some values for font, size, etc. but I would like this to be an exact copy of the formatting for \section. The only change I need is to be able to change the text in the section title. Are there any commands which can extract this for me?

Or is the proper way to just renew the command in the part of the text where I need the custom section, and restore it afterwards?

1

I don't know about extracting the exact style from the default \section command, but it seems like titlesec doesn't provide facilities for this and doing it manually or automatically would be fragile.

However, just locally changing the title label seems to be easy enough, and by using a TeX group (that's the inner layer of braces), it automatically changes back to the default when we're done.

\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}

result

EDIT: In an older verion of this post, two more complicated versions were shown that either manually reset \titlelabel to the default or that accessed internal LaTeX macros to save and restore it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.