0

I want to redefine \maketitle the way it contains my custom command. I have a home assignment and I'd like to put \@variant command or something to be able to set it later as with \@author and \@title.

Here is my redefinition

\makeatletter
\renewcommand{\maketitle}{
    \begin{center}
        \vspace{2ex}
        \textbf{\huge \textsc{\@title}}
        \vspace{3ex}\\
        \Large \@author\\
        \hrulefill\\
        \vspace{1ex}
    \end{center}
}
\makeatother

Then in preambule I have such commands:

\title{My beautiful title}
\author{My Name}

And I want to write \varian{number} after \author and get it right under my name.

Here is the definition of the \variant:

\newcommand{\variant}[1]{Variant №\:#1}

And it should be something like that:

\makeatletter
\renewcommand{\maketitle}{
    \begin{center}
        \vspace{2ex}
        \textbf{\huge \textsc{\@title}}
        \vspace{3ex}\\
        \Large \@author\\
        \small \@variant\\
        \hrulefill\\
        \vspace{1ex}
    \end{center}
}
\makeatother

\title{My beautiful title}
\author{My Name}
\variant{number}

Important: I don't want to hard code it straight away in the redefinition, I want to make everything fancy. And I don't want to use any additional packages.

1

2 Answers 2

1

The \variant command can be defined as:

\newcommand\variant[1]{\renewcommand\@variant{Variant \textnumero\,#1}}
\newcommand\@variant{}

I added thetextcomp package to typeset the № symbol. Otherwise this character is not recognized. If you don't want to add this package, maybe change the Number identification.

\documentclass[]{article}

\usepackage{textcomp}

\makeatletter
\newcommand\variant[1]{\renewcommand\@variant{Variant \textnumero\,#1}}
\newcommand\@variant{}
\renewcommand{\maketitle}{
    \begin{center}
        \vspace{2ex}
        \textbf{\huge \textsc{\@title}}
        \vspace{3ex}\\
        \Large \@author\\
        \small \@variant\\
        \hrulefill\\
        \vspace{1ex}
    \end{center}
}
\makeatother

\begin{document}
\title{My beautiful title}
\author{My Name}
\variant{number}
\maketitle
\end{document}

enter image description here

4
  • 1
    Perfect! Thank you very much, exactly what I wanted!
    – antshar
    Apr 27, 2020 at 14:56
  • Also the other answer is worth upvoting ;) Apr 27, 2020 at 14:58
  • Can you explain how it works exactly? Why do you need to define \@variant empty and then redefine it? Can't it be defined right into \variant definition?
    – antshar
    Apr 27, 2020 at 15:12
  • 1
    @antshar It's not a good idea use \newcommand inside such a macro, so you need to use \renewcommand if you are doing this using only LaTeX constructs. Since \renewcommand assumes the existence of the command, you need to define it initially with \newcommand.
    – Alan Munn
    Apr 27, 2020 at 15:15
0

The titling package provides a nice interface for modifying the title. It provides commands to insert material after each of the required elements.

Here's a simple way to add your \variant to the title:

\documentclass{article}
\usepackage{titling}
\newcommand{\variant}[1]{\def\myvariant{#1}}
\renewcommand{\maketitlehookc}{\large\centering\par  Variant No: \myvariant}
\title{A title}
\variant{A variant number}
\author{An author}
\date{\today}
\begin{document}
\maketitle
\end{document}

output of code

2
  • At the end of the question I asked to offer the solution without any additional packages. Nevertheless, thank you for the answer, I'll take into accout such a method.
    – antshar
    Apr 27, 2020 at 14:55
  • @antshar Yes, I understand. I think the "no extra packages" requirement is overrated. If you are writing your own document class or package, then modifying large chunks of code from the core classes may be appropriate, but if you're doing this within an individual document, this is a much cleaner approach. I'll leave it here for others.
    – Alan Munn
    Apr 27, 2020 at 15:00

You must log in to answer this question.

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