1

I'm using the extarticle class and I want to have a \subtitle command to add subtitles for sections. Here's what I expected: Expected effect

Here is my current solution, quite ugly to be frank:

\documentclass{extarticle}
\usepackage{titlesec}

\makeatletter
\newlength{\fsizept}
\setlength{\fsizept}{\f@size pt}
\makeatother

\newlength{\titleruleheight}
\setlength{\titleruleheight}{0.075\fsizept}

\titleformat{\section}{\Large\sffamily}
    {\thesection}{1em}{}[{\titlerule[\titleruleheight]}]
\newlength{\subtitleposfix}
\setlength{\subtitleposfix}{-2\baselineskip} %This should be linked with font size.
\newcommand\subtitle[1]{
    \vspace{\subtitleposfix}%
    \begin{flushright}%
        \emph{\emph{------}#1}%
    \end{flushright}\par}
\begin{document}
    \section{The title}
    \subtitle{The subtitle}
\end{document}

I find it hard to correctly align the title and subtitle. The 2\baselineskip above is just an inaccurate approximation. The real value should depend on font size and configurations of the titlerule (as you can see, this titlerule depends on the font size given by \fsizept).

I should add that I did some searches for existing questions such as this one. But I didn't find a satisfied solution for my case.

Anyway, is there a better way to achieve this?

1 Answer 1

0

Here is my way for subtitle based to my answer here:

\documentclass[10pt]{article}
\usepackage{titlesec}

\makeatletter
\newlength{\fsizept}
\newlength{\oldfsizept}
\newlength{\oldbaselineskip}
\newlength{\titleruleheight}
\newcommand{\readfsize}{%
\setlength{\fsizept}{\f@size pt}%
\setlength{\titleruleheight}{0.075\fsizept}%
}
\newcommand{\storeoldfsize}{%
\setlength{\oldfsizept}{\f@size pt}%
\setlength{\oldbaselineskip}{\baselineskip}%
\setlength{\titleruleheight}{0.075\oldfsizept}%
}
\makeatother






\newlength{\oldparskip}

\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\StarredSection}{\NonStarredSection}%
}
\def\StarredSection{%
\@ifnextchar[
{\StarredSectionWith}%
{\StarredSectionWithout}%
}
\def\NonStarredSection{%
\@ifnextchar[
{\NonStarredSectionWith}%
{\NonStarredSectionWithout}%
}
\def\StarredSectionWith[#1]#2{%
\@ifnextchar[
{\StarredSectionWithWith[#1]#2}%
{\StarredSectionWithWithout[#1]{#2}}%
}
\def\StarredSectionWithout#1{%
\@ifnextchar[
{\StarredSectionWithoutWith#1}%
{\StarredSectionWithoutWithout{#1}}%
}
\def\NonStarredSectionWith[#1]#2{%
\@ifnextchar[
{\NonStarredSectionWithWith[#1]#2}%
{\NonStarredSectionWithWithout[#1]{#2}}%
}
\def\NonStarredSectionWithout#1{%
\@ifnextchar[
{\NonStarredSectionWithoutWith#1}%
{\NonStarredSectionWithoutWithout{#1}}%
}
\def\StarredSectionWithWith[#1]#2[#3]{%
\def\mysubtitle{#3}%
\oldsection*{#2}
}
\def\StarredSectionWithoutWith#1[#2]{%
\def\mysubtitle{}%
\def\mysubtitle{#2}%
\oldsection*{#1}
}
\def\StarredSectionWithWithout[#1]#2{%
\def\mysubtitle{}%
\oldsection*{#2}
}
\def\StarredSectionWithoutWithout#1{%
\def\mysubtitle{}%
\oldsection*{#1}
}
\def\NonStarredSectionWithWith[#1]#2[#3]{%
\def\mysubtitle{#2}%
\oldsection[#1]{#2}
}
\def\NonStarredSectionWithoutWith#1[#2]{%
\def\mysubtitle{#2}%
\oldsection{#1}%
}
\def\NonStarredSectionWithWithout[#1]#2{%
\def\mysubtitle{}%
\oldsection[#1]{#2}
}
\def\NonStarredSectionWithoutWithout#1{%
\def\mysubtitle{}%
\oldsection{#1}
}

\titleformat{\section}{\Large\sffamily}
    {\thesection\readfsize}{1em}{}[{\storeoldfsize\vspace{-\dimexpr\oldbaselineskip}{\hfill\small\emph{\ifx\mysubtitle\empty\relax\else------\fi\mysubtitle}}\vspace{-\dimexpr0.7\oldbaselineskip}\linebreak\readfsize\rule{\linewidth}{\titleruleheight}}]

\makeatother


\begin{document}
    \section{The title}[My subtitle]
    Test text

\section{Title}{}
[This is just a text in square brackets]

\end{document}

This way the subtitle is an extra optional argument of section after the mandatory and you may add text in square brackets as done in my second section of the example.

enter image description here

5
  • Your solution is good, but sometimes there isn't a subtitle yet it still shows "--My subtitle". Is there a way to show nothing when there's no subtitle?
    – Jinwen
    Aug 24, 2019 at 4:53
  • Also, the space between title texts and the rule is a little bit too large. I see you use a \vspace{-\baselineskip} but this changes as the font size changes, so the final result would be unstable. Is there another way to just resembles my original \titlerule?
    – Jinwen
    Aug 24, 2019 at 5:03
  • @XuJinwen .. Thanks ... didn't really tested ... just wanted to show the way ... Fixed now!
    – koleygr
    Aug 24, 2019 at 8:47
  • Your current solution works perfect so I have accepted it. I just wonder two things: 1) Why is there a \storeoldfsize? I didn't see the original value of \baselineskip and fsizept get changed. 2) What does the \dimexpr do? It seems fine removing them.
    – Jinwen
    Aug 24, 2019 at 10:23
  • @XuJinwen ... 1) I store the values before the font size is been changed in \small to use these values and the new ones. They do change ... See for example what will happen if you add \fontsize{5}{6}\selectfont instead of my \small command for the subtitle. 2) \dimexpr left there because I was making some additions etc of lengths... but realized that doesn't needed ... I would leave it there in case that an extra constant length would be used for the vertical spaces.
    – koleygr
    Aug 24, 2019 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .