2

This is a follow-up question to: Different titleformat style for different numberless chapters

I have something like the following macro:

\titleformat{\chapter}
  {\normalfont\Huge\color{blue}}
  {}
  {0pt}
  {%
    \begin{minipage}{.4\textwidth}
      #1
    \end{minipage}
  }

The #1 comes from the [explicit] option of the titlesec package. If I try to put the previous code inside another macro, like this:

\newcommand{\mycustomchapter}{
  \titleformat{\chapter}
   %...
}

compilations fails complaining about the argument.

Is it possible to hide the \titleformat macro in another macro when the explicit option is in place?

MWE

\documentclass{book}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\newcommand{\mycustomchapter}{
  \titleformat{\chapter}
    {\normalfont\Huge\color{blue}}
    {}
    {0pt}
    {%
      \begin{minipage}{.4\textwidth}
        #1
      \end{minipage}
    }
}

\begin{document}
  \mycustomchapter
  \chapter{My chapter}
  Bla bla bla 
\end{document}
lockstep
  • 250,273
Lupen
  • 179

1 Answers1

2

Use ##1 instead of #1. However, the explicit option is very rarely needed.

Better is like below: the last argument to \titleformat can end with a macro taking one argument, which will be fed the title.

\documentclass{book}
\usepackage{xcolor}
\usepackage{titlesec}

\newcommand{\mycustomchapter}{%
  \titleformat{\chapter}
    {\normalfont\Huge\color{blue}}
    {}
    {0pt}
    {\mycustomchapterformat}%
}
\newcommand{\mycustomchapterformat}[1]{%
   \begin{minipage}{.4\textwidth}
   #1
   \end{minipage}%
}

\begin{document}
  \mycustomchapter
  \chapter{My chapter}
  Bla bla bla 
\end{document}

enter image description here

egreg
  • 1,121,712