4

This is the package https://github.com/mliu7/latex-moderncv

I'm trying to learn how to make packages and classes something about this package makes me confused.

On line 380

\newcommand*{\makecvtitle}{}

This makes no sense because there are no parameters however in the TeX document if you type \makecvtitle it creates a lovely title with name address and other info.

Please explain black magic that allows this command to make a cv title

2 Answers 2

4

You'll find numerous such empty definitions inside moderncv.cls; not just for the title.

The structure of moderncv is such that it defines (via \newcommand) certain fundamental constructs when you load the class

\documentclass[..]{moderncv}

Then, depending on the choice of style you want, you would call

\moderncvstyle{<style>}

The above is defined as

\newcommand*{\moderncvstyle}[1]{
  \RequirePackage{moderncvstyle#1}}

So, with a call to \moderncvstyle{casual}, moderncv loads the "package" moderncvstylecasual.sty, which redefines \moderncv according to its own style via

\renewcommand{\makecvtitle}{...}

This is one approach to writing a class: The class sets up some fundamental constructions which are then extended by loading packages related to the class; an initial \newcommand which is then almost immediately changed via \renewcommand.

Are there any advantages to this approach? Yes. It allows the user to call the class without loading the extensions (or packages) yet still use the basic foundation. You can now modify the construction using a standard approach: \renewcommand{\makecvtitle}{...}.

Any disadvantages? See above... :-| I guess it depends on the end-user. You won't know to use \newcommand{\makecvtitle}{...} or \renewcommand{\makecvtitle}{...} until you run into a problem.

A way to circumvent this (again, from the end-user's perspective) if you want to define your own styles, would be to use a TeX-like syntax; that is

\def\makecvtitle{...}

since this will overwrite the definition of \makecvtitle regardless of its existence.

2
  • This is very similar to David's answer, just with a little more mortar.
    – Werner
    Commented Feb 13, 2015 at 1:41
  • 1
    I liked the mortar
    – Howard
    Commented Feb 13, 2015 at 17:53
5

That is just a default (which does nothing) It gets redefined depending on options used, for example

moderncv/moderncvstylebanking.sty

does

\renewcommand*{\makecvtitle}{%
  % recompute lengths (in case we are switching from letter to resume, or vice versa)
  \recomputecvlengths%
  \maketitle%
  % optional quote
  \ifthenelse{\isundefined{\@quote}}%
    {}%
    {{\centering\begin{minipage}{\quotewidth}\centering\quotestyle{\@quote}\end{minipage}\\[2.5em]}}%
  \par}% to avoid weird spacing bug at the first section if no blank line is left after \maketitle}

which makes it do something.

2
  • OP, note the use of \renew... since the command was already defined.
    – Sigur
    Commented Feb 13, 2015 at 1:49
  • Thank you for some reasons I missed the redefine in that style file. Would you consider this process a "best Practice" for making classes and packages?
    – Howard
    Commented Feb 13, 2015 at 17:49

You must log in to answer this question.

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