1

I am currently doing the following in order to obtain a rule below each section title in a KOMA Article.

\documentclass[]{scrartcl}

% RULE BELOW SECTION TITLES

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
\ifstr{#1}{section}{%
    \parbox[t]{\linewidth}{%
      \raggedsection\@hangfrom{\hskip #2#3}{#4}\par%
      \kern-1.00\ht\strutbox\rule{\linewidth}{.8pt}%
    }%
  }{%
    \@hangfrom{\hskip #2#3}{#4}}% 
}
\makeatother

\begin{document}

\section{Whatever}

\subsection{Whatever else}

\end{document}

This is the output:

enter image description here

I have tried to modify this approach to obtain a dotted rule below every subsection title, but I haven’t succeeded.

Could you please help me generate a dotted rule below every subsection title?

2 Answers 2

2

You could use package dashrule:

\documentclass[]{scrartcl}

\usepackage{dashrule}

% RULE BELOW SECTION TITLES

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
    \parbox[t]{\linewidth}{%
      \raggedsection\@hangfrom{\hskip #2#3}{#4}\par%
      \rule[1.5ex]{\linewidth}{.8pt}%
    }%
  }{%
    \Ifstr{#1}{subsection}{%
    \parbox[t]{\linewidth}{%
    \raggedsection\@hangfrom{\hskip #2#3}{#4}\par
    \hdashrule[1.5ex]{\linewidth}{.8pt}{.8pt}}}%
    {\@hangfrom{\hskip #2#3}{#4}\par}%%
    }%
}
\makeatother

\begin{document}

\section{Whatever}

\subsection{Whatever else}

\end{document}

I hope that is what you asked for:

13
  • 1
    @cabohah you're right. I misread it...
    – lukeflo
    Commented Sep 21, 2023 at 9:09
  • 2
    @cabohah, was not clear from the OP's question. But i added the necessary condition to print the lines only below subsection.
    – lukeflo
    Commented Sep 21, 2023 at 9:41
  • 1
    @Hector I also edited the code of your rule, so that both rules are raised by the same dimen. Of course, you can adjust it.
    – lukeflo
    Commented Sep 21, 2023 at 9:46
  • 1
    Just a typo, deleted a brace. Corrected
    – lukeflo
    Commented Sep 21, 2023 at 9:53
  • 1
    Thank you. Your solution works. I have therefore accepted it.
    – EoDmnFOr3q
    Commented Sep 21, 2023 at 9:55
2

Here is a suggestion similar to @lukeflo's, but using LaTeX3's \str_case:nn instead of KOMA-Script's \ifstr (deprecated) or \Ifstr:

\documentclass[]{scrartcl}

\usepackage{blindtext}

\usepackage{dashrule}

%\usepackage{expl3}% should not be needed any longer, but if you are
                  % using a very old LaTeX installation, you can activate it

% RULE BELOW SECTION TITLES

\ExplSyntaxOn
\makeatletter
\renewcommand{\sectionlinesformat}[4]{
  \str_case:nnF { #1 }
    {
      { section }
        {
          \parbox[t]{\linewidth}{
            \raggedsection\@hangfrom{\hskip #2#3}{#4}\par
            \kern-1.00\ht\strutbox\rule{\linewidth}{.8pt}
          }
        }
      { subsection }
        {
          \parbox[t]{\linewidth}{
            \raggedsection\@hangfrom{\hskip #2#3}{#4}\par
            \hdashrule[2ex]{\linewidth}{.8pt}{.8pt}
          }
        }
    }
    {
      \@hangfrom{\hskip #2#3}{#4}
    }
}
\makeatother
\ExplSyntaxOff

\begin{document}

\section{Example of section}
\blindtext

\subsection{Example of subsection}
\blindtext

\subsubsection{Example of subsubsection}
\blindtext

\paragraph{Example of paragraph}
\blindtext

\subparagraph{Example of subparagraph}
\blindtext

\end{document}

enter image description here

See The LaTeX3 Interfaces for more information about LaTeX3 and functions like \str_case:nn.

3
  • Personally I would prefer your version, since LaTeX3 seems simply more straightforward in most cases! +1
    – lukeflo
    Commented Sep 21, 2023 at 9:47
  • @lukeflo Yes, LaTeX3 offers nice features. But in this case it is not really LaTeX3 code, but only using one LaTeX3 feature. Real LaTeX3 code should IMHO also replace, e.g., \parbox or at least \hskip. So IMHO it is a quite ugly mixture. However this kind it is easier to understand for l3 beginners.
    – cabohah
    Commented Sep 21, 2023 at 9:53
  • 1
    But in many cases its still a mixture of latex2e and latex3 source code, since most packages aren't relying heavily on latex3 up-do-date. At the moment I'm converting much of my personal stuff to latex3, but AFAIK sometimes there are features which are not yet fully implemented in latex3. But thats way off-topic... ;)
    – lukeflo
    Commented Sep 21, 2023 at 9:57

You must log in to answer this question.

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