3

I want make my own style of titles(section/subsection... etc.) by the package titlesec. The title name is specified by a macro. But it fails to pass compile. This do is a problem also for the other arguments of \titleformat. Why? Below is my MWE:

\documentclass{article}
\usepackage{titlesec,xcolor}
\begin{document}
\def\titlename{section}
\titleformat{\csname \titlename\endcsname}
{\normalfont\Large\bfseries\color{red}}{\the\titlename}{1em}{}
\end{document}
5
  • The problem is that things need to get expanded in a different order. See tex.stackexchange.com/a/239555/121799 for an example how that may be done.
    – user121799
    Commented Feb 18, 2019 at 4:38
  • What are you trying to obtain, exactly?
    – Bernard
    Commented Feb 18, 2019 at 9:00
  • To define my style of title section, or subsection...etc. For example, \settitle{name=section, format=...,label=..., ...}.
    – lyl
    Commented Feb 18, 2019 at 11:46
  • Can you instead present what the desired format for \settitle is, with the correspondence between keys and arguments for \titleformat?
    – egreg
    Commented Feb 18, 2019 at 12:14
  • @egreg As it's hard to remember the meaning of every argument in \titleformat and \titlespacing, I decided to give names to all these args , that is to say, \settitle{name=section, format=...,label=..., left=..., before title text=...}.
    – lyl
    Commented Feb 19, 2019 at 0:47

1 Answer 1

2

Here's a key-value system for titlesec:

\documentclass{article}
\usepackage{titlesec}
\usepackage{xparse}

\ExplSyntaxOn

\clist_new:N \l__lyl_titlesec_main_clist

\keys_define:nn { lyl/titlesec }
 {
  name       .code:n   = \clist_put_right:Nx \l__lyl_titlesec_main_clist
                          { name = \exp_not:c {#1} },
  page       .code:n   = \clist_put_right:Nn \l__lyl_titlesec_main_clist
                          { page = #1 },
  numberless .code:n   = \clist_put_right:Nn \l__lyl_titlesec_main_clist
                          { numberless },
  shape      .tl_set:N = \l__lyl_titlesec_shape_tl,
  format     .tl_set:N = \l__lyl_titlesec_format_tl,
  label      .tl_set:N = \l__lyl_titlesec_label_tl,
  sep        .tl_set:N = \l__lyl_titlesec_sep_tl,
  before     .tl_set:N = \l__lyl_titlesec_before_tl,
  after      .tl_set:N = \l__lyl_titlesec_after_tl,
  unknown    .code:n   = \keys_set:nn { lyl/titlesec } { name = #1 }
 }

\NewDocumentCommand{\lyltitleformat}{m}
 {
  % start afresh
  \clist_clear:N \l__lyl_titlesec_main_clist
  \keys_set:nn { lyl/titlesec }
   {
    shape = hang, format = , label = , sep = 0pt, before = , after = ,
   }
  % set the keys
  \keys_set:nn { lyl/titlesec } { #1 }
  % define the format
  \lyl_titlesec_set:VVVVVVV
   \l__lyl_titlesec_main_clist
   \l__lyl_titlesec_shape_tl
   \l__lyl_titlesec_format_tl
   \l__lyl_titlesec_label_tl
   \l__lyl_titlesec_sep_tl
   \l__lyl_titlesec_before_tl
   \l__lyl_titlesec_after_tl
 }

\cs_new_protected:Nn \lyl_titlesec_set:nnnnnnn
 {
  \titleformat{#1}[#2]{#3}{#4}{#5}{#6}[#7]
 }
\cs_generate_variant:Nn \lyl_titlesec_set:nnnnnnn { VVVVVVV }

\ExplSyntaxOff

\lyltitleformat{
  name = section,
  numberless,
  shape = hang,
  format = \large\bfseries,
  before = \MakeUppercase,
}
\lyltitleformat{
  name = section,
  shape = hang,
  format = \large\itshape,
  label = \thesection,
  sep = 1em,
  before = \adddashes,
}
\lyltitleformat{
  subsection,
  shape=runin,
  format=\scshape,
  label=(\thesubsection)~,
}

\newcommand{\adddashes}[1]{---#1---}

\begin{document}

\section*{Introduction}

abc def

\section{First}

abc def

\subsection{Xyz}

abc def

\end{document}

The level (or name) can be given either as value to name or without qualification. If you use the explicit option, you can use #1 in the value for before.

There are two default values: hang for shape and 0pt for sep.

0

You must log in to answer this question.

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