A few notes on your code:
- use
\par within the group in which font changes are applied, so instead of {\huge Title string}\par use {\huge Title string\par} or else the interline spacing will end up wrong.
- you had a few unwanted spaces in your definition, note that a space after
#1 in your definition is not ignored, even if #1 will be \huge or something like that at use time.
- your variables aren't following
expl3 naming conventions, I suggest you correct this (those conventions are there for a reason, even if there is no sane way to make TeX enforce them).
I didn't correct all of those in each code block, so don't just copy these 1:1 but pay attention where you need to fix them.
The issue is that you redefine \@makechapterhead inside of a group and the definition is lost at its end. You have to make the redefinition after \group_end:, you could do this like the following. Also, your \coolchap macro does throw an error if you omit the optional argument, you should use O{} instead of o like in the following.
\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}
\makeatletter
\NewDocumentCommand{\chapterformat}{mmmmm}{%
\ifdef{\chapter}{%
\renewcommand{@makechapterhead}[1]{%
\vspace*{#3\p@}% Vertical Space before "Chapter X" (50)
{\parindent \z@ \raggedright \reset@font
\ifnum \c@secnumdepth >\m@ne
{#1 @chapapp{} \thechapter} % Chapter-word formatting.
\par
\vskip #4\p@ % Vertical Space after "Chapter X" (20)
\fi
{#2 ##1} % Title formatting.
\par\nobreak
\vskip #5\p@}} % Vertical Space after Chapter Title (180)
}{}
}
\makeatother
\ExplSyntaxOn
\tl_new:N \l__xcoolchap_redefinition_tl
\NewDocumentCommand{\coolchap}{O{}}
{
\group_begin:
\keys_set:nn { coolchap } { #1 }
\tl_set:Nx \l__xcoolchap_redefinition_tl
{
\exp_not:N \chapterformat
{ \exp_not:V \l__xcoolchap_chapstyle_tl }
{ \exp_not:V \l__xcoolchap_titlestyle_tl }
{ \exp_not:V \l__xcoolchap_befchap_tl }
{ \exp_not:V \l__xcoolchap_afchap_tl }
{ \exp_not:V \l__xcoolchap_aftitle_tl }
}
\exp_last_unbraced:NV
\group_end:
\l__xcoolchap_redefinition_tl
}
\keys_define:nn { coolchap }
{
chapstyle .tl_set:N = \l__xcoolchap_chapstyle_tl,
titlestyle .tl_set:N = \l__xcoolchap_titlestyle_tl,
befchap .tl_set:N = \l__xcoolchap_befchap_tl,
afchap .tl_set:N = \l__xcoolchap_afchap_tl,
aftitle .tl_set:N = \l__xcoolchap_aftitle_tl,
chapstyle .initial:n = \HUGE\bfseries,
titlestyle .initial:n = \Huge\bfseries,
befchap .initial:n = 50,
afchap .initial:n = 20,
aftitle .initial:n = 180,
}
\ExplSyntaxOff
\begin{document}
\coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
\chapter{essai}
XXX
\end{document}
Disclaimer: I'm the author of expkv-cs
Note that this is a lot easier with expkv-cs since it doesn't work with assignments and hence has no such grouping issues:
\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}
\usepackage{expkv-cs}
\makeatletter
\NewDocumentCommand{\chapterformat}{mmmmm}{%
\ifdef{\chapter}{%
\renewcommand{@makechapterhead}[1]{%
\vspace*{#3\p@}% Vertical Space before "Chapter X" (50)
{\parindent \z@ \raggedright \reset@font
\ifnum \c@secnumdepth >\m@ne
{#1 @chapapp{} \thechapter} % Chapter-word formatting.
\par
\vskip #4\p@ % Vertical Space after "Chapter X" (20)
\fi
{#2 ##1} % Title formatting.
\par\nobreak
\vskip #5\p@}} % Vertical Space after Chapter Title (180)
}{}
}
\NewDocumentCommand\coolchap{O{}}{\coolchapKV{#1}}
\ekvcSplitAndForward\coolchapKV\chapterformat
{
chapstyle = \HUGE\bfseries
,titlestyle = \Huge\bfseries
,befchap = 50
,afchap = 20
,aftitle = 180
}
\makeatother
\begin{document}
\coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
\chapter{essai}
XXX
\end{document}
Output of both codes:

A different approach could be to just redefine \chapterformat and use your variables inside of that redefinition. That way you don't have to use any expansion tricks whatsoever and continue to use l3keys:
\documentclass{report}
\usepackage{etoolbox}
\usepackage{moresize}
\makeatletter
\ExplSyntaxOn
\ifdef{\chapter}{%
\renewcommand{@makechapterhead}[1]
{%
\vspace*{\l__xcoolchap_befchap_tl\p@}% Vertical Space before "Chapter X" (50)
\begingroup
\parindent \z@ \raggedright \reset@font
\ifnum \c@secnumdepth >\m@ne
{\l__xcoolchap_chapstyle_tl @chapapp{} \thechapter\par}% Chapter-word formatting.
\vskip \l__xcoolchap_afchap_tl\p@ % Vertical Space after "Chapter X" (20)
\fi
{\l__xcoolchap_titlestyle_tl #1\par}% Title formatting.
\nobreak
\vskip \l__xcoolchap_aftitle_tl\p@
\endgroup
}% Vertical Space after Chapter Title (180)
}{}
\makeatother
\NewDocumentCommand{\coolchap}{O{}}
{ \keys_set:nn { coolchap } { #1 } }
\keys_define:nn { coolchap }
{
chapstyle .tl_set:N = \l__xcoolchap_chapstyle_tl,
titlestyle .tl_set:N = \l__xcoolchap_titlestyle_tl,
befchap .tl_set:N = \l__xcoolchap_befchap_tl,
afchap .tl_set:N = \l__xcoolchap_afchap_tl,
aftitle .tl_set:N = \l__xcoolchap_aftitle_tl,
chapstyle .initial:n = \HUGE\bfseries,
titlestyle .initial:n = \Huge\bfseries,
befchap .initial:n = 50,
afchap .initial:n = 20,
aftitle .initial:n = 180,
}
\ExplSyntaxOff
\begin{document}
\coolchap[aftitle=36,befchap=10,afchap=40,chapstyle=\HUGE\itshape]
\chapter{essai}
XXX
\end{document}
egreg's second proposal contains the redefinition inside the group and there is no issue?? I took it as a model and now I'm lost. Wow,explkv-csliterally takes two lines to create the command, it's tremendously impressive... – Vincent Krebs Jan 22 '23 at 17:41expkv-csshines, for many other thingsl3keysis much better suited (the right tool for the right job and stuff... :) --expkv-csis brilliant to split a handful of keys into separate arguments and that's about it, for everything more complicated use either the general purpose key types inexpkv-defor another nice solution likel3keys). – Skillmon Jan 22 '23 at 17:45\drawcode. Note that this works inside of the group since TikZ is created in a way that it works with nested groups (e.g. itsscopeenvironment, or thepics, ornodes, etc.). – Skillmon Jan 22 '23 at 17:47\let\split\ekvcSplitAndForwardis not a good idea, imho, you save a few characters and get possible incompatibilities with other code that relies on some\splitmacro. – Skillmon Jan 22 '23 at 18:08SplitAndFowarddoesn't sound too complicated,ekvcis just the naming-prefix ofexpkv-csto avoid collisions. – Skillmon Jan 22 '23 at 18:11\@makechapterheadthen you shouldn't code your macro in the first place :P – Skillmon Jan 22 '23 at 18:12\ekvcSplitand\ekvcSplitAndUse(the last name of these is probably the most annoying one of these). – Skillmon Jan 22 '23 at 18:14\NewDocumentCommand\coolchap{O{}}{\coolchapKV{#1}}is not even needed. Only 1 command to achieve what I need, thanksexpkv. – Vincent Krebs Jan 22 '23 at 18:19\splitis used inamsmath– Vincent Krebs Jan 22 '23 at 18:38