[Tex/LaTex] Changing parameters memoir chapter styles in newcommands

chaptersmemoir

How do i turn the whole parameter below that is between the %%%% in a \newcommand? I tried but it is giving error.

\documentclass[a4paper]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{color,calc,graphicx,soul,fourier,lipsum}

%%%%%%%%%%%
% transform in newcommand
\makeatletter
  \newlength\dlf@normtxtw
  \setlength\dlf@normtxtw{\textwidth}
  \def\myhelvetfont{\def\sfdefault{mdput}}
  \newsavebox{\feline@chapter}
  \newcommand\feline@chapter@marker[1][4cm]{%
    \sbox\feline@chapter{%
      \resizebox{!}{#1}{\fboxsep=1pt%
      \colorbox{nicered}{\color{white}\bfseries\sffamily\thechapter}%
    }
  }%
  \rotatebox{90}{%
  \resizebox{%
    \heightof{\usebox{\feline@chapter}}+\depthof{\usebox{\feline@chapter}}}%
    {!}{\scshape\so\@chapapp}}\quad%
    \raisebox{\depthof{\usebox{\feline@chapter}}}{\usebox{\feline@chapter}}%
  }
  \newcommand\feline@chm[1][4cm]{%
    \sbox\feline@chapter{\feline@chapter@marker[#1]}%
    \makebox[0pt][l]{% aka \rlap
    \makebox[1cm][r]{\usebox\feline@chapter}%
    }
  }
  \makechapterstyle{daleif1}{
  \renewcommand\chapnamefont{\normalfont\Large\scshape\raggedleft\so}
  \renewcommand\chaptitlefont{\normalfont\huge\bfseries\scshape\color{nicered}}
  \renewcommand\chapternamenum{}
  \renewcommand\printchaptername{}
  \renewcommand\printchapternum{\null\hfill\feline@chm[2.5cm]\par}
  \renewcommand\afterchapternum{\par\vskip\midchapskip}
  \renewcommand\printchaptertitle[1]{\chaptitlefont\raggedleft ##1\par}
  }
\makeatother
%%%%%%%%%%%

% sugestion
% \newcommand{\mydaleif}
\mydaleif
\chapterstyle{daleif1}

\begin{document}

\chapter{Daleif1 Chapter Style}

\lipsum

\end{document}

Best Answer

Here a realisation of your "suggestion":

enter image description here

\documentclass[a4paper]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{color,calc,graphicx,soul,fourier,lipsum}

%%%%%%%%%%%
% transformed into a \newcommand
\makeatletter
\newcommand{\mydaleif}{%
  \newlength\dlf@normtxtw
  \setlength\dlf@normtxtw{\textwidth}
  \def\myhelvetfont{\def\sfdefault{mdput}}
  \newsavebox{\feline@chapter}
  \newcommand\feline@chapter@marker[1][4cm]{%
    \sbox\feline@chapter{%
      \resizebox{!}{##1}{\fboxsep=1pt%
      \colorbox{red}{\color{white}\bfseries\sffamily\thechapter}%
    }
  }%
  \rotatebox{90}{%
  \resizebox{%
    \heightof{\usebox{\feline@chapter}}+\depthof{\usebox{\feline@chapter}}}%
    {!}{\scshape\so\@chapapp}}\quad%
    \raisebox{\depthof{\usebox{\feline@chapter}}}{\usebox{\feline@chapter}}%
  }
  \newcommand\feline@chm[1][4cm]{%
    \sbox\feline@chapter{\feline@chapter@marker[##1]}%
    \makebox[0pt][l]{% aka \rlap
    \makebox[1cm][r]{\usebox\feline@chapter}%
    }
  }
  \makechapterstyle{daleif1}{
  \renewcommand\chapnamefont{\normalfont\Large\scshape\raggedleft\so}
  \renewcommand\chaptitlefont{\normalfont\huge\bfseries\scshape\color{red}}
  \renewcommand\chapternamenum{}
  \renewcommand\printchaptername{}
  \renewcommand\printchapternum{\null\hfill\feline@chm[2.5cm]\par}
  \renewcommand\afterchapternum{\par\vskip\midchapskip}
  \renewcommand\printchaptertitle[1]{\chaptitlefont\raggedleft ####1\par}
  }
}
\makeatother
%%%%%%%%%%%

\mydaleif
\chapterstyle{daleif1}

\begin{document}

\chapter{Daleif1 Chapter Style}

\lipsum

\end{document}

Here are some considerations:

  • Although the internals of the macro may require some @ usage and therefore a \makeatletter and \makeatother modification pair, they should be used outside the macro definition.

  • When you're defining a command within another command and the "inner command" takes parameters as arguments, then the use of argument numbers via #1, #2, etc. has to change. Instead, you need to use ##1, ##2, etc.

  • Macros are meant to encapsulate some group of commands that might be used often, and therefore typically take parameters or arguments to allow for some future modification. This is not the case with your current \mydaleif. As such, I don't see the use of such a macro, since you call it immediately after defining it, and it doesn't serve much use after it. However, this may only be because I don't know the actual use-case.

  • Defining commands within some scope or group has its limitations. For example, consider the two macros:

    \newcommand{\mycmdone}{\newcommand{\testone}{Hello world 1.}}
    \newcommand{\mycmdtwo}{{\newcommand{\testtwo}{Hello world 2.}}}
    

    Executing \mycmdone and then \testone will print Hello world 1., while executing \mycmdtwo and \testtwo will signal an error (Undefined control sequence), since \testtwo was defined inside a group. Note that \mycmdone is literally replaced by \newcommand{..}{...} while \mycmdtwo is replaced by {\newcommand{..}{...}}. The same holds for \renewcommand or other definitions.

Since you didn't include your definition of nicered, I just changed it to red instead.

Related Question