I would like to have my chapter and section headings at the center of page.Currently they are at left.Please give me some suggestion without using any packages please.
1 Answer
You need to redefine \@makechapterhead
(for numbered chapters) and \@makeschapterhead
(for unnumbered chapters) and \section
(the original definitions can be found in report.cls
):
\documentclass{report}
\usepackage{lipsum}% just for the example
\makeatletter
\def\@makechapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\centering\huge\bfseries \@chapapp\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\interlinepenalty\@M
\Huge \bfseries #1\par\nobreak
\vskip 40\p@
}}
\def\@makeschapterhead#1{%
\vspace*{50\p@}%
{\parindent \z@ \raggedright
\normalfont
\interlinepenalty\@M
\centering\Huge \bfseries #1\par\nobreak
\vskip 40\p@
}}
\renewcommand\section{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\centering\normalfont\Large\bfseries}}
\makeatother
\begin{document}
\chapter{A test chapter title}
\section{A test section title}
\lipsum[4]
\end{document}
Patching the commands, the code would be significatively shorter:
\documentclass{report}
\usepackage{etoolbox}
\usepackage{lipsum}% just for the example
\makeatletter
\patchcmd{\@makechapterhead}
{\huge}
{\centering\huge}
{}
{}
\patchcmd{\@makechapterhead}
{\huge}
{\centering\huge}
{}
{}
\patchcmd{\section}
{\normalfont}
{\centering\normalfont}
{}
{}
\makeatother
\begin{document}
\chapter{A test chapter title}
\section{A test section title}
\lipsum[4]
\end{document}
Since the requirement was not to use additional packages, I included the redefinition as first option.
-
1For completeness, and for the benefit of anyone who may want to replicate this effort, you may want state where the original source code may be found.– MicoCommented Jun 22, 2015 at 15:20
-
-
While I think there's a case for giving people a hand if they are really stuck with homework, I think providing complete solutions is bad. I don't think it helps anybody in the end. Sorry. I am aware that not everybody here shares my perspective.– cfrCommented Jun 22, 2015 at 18:53
-
Although I admit that this sounds, prima facie, like a rather bad homework assignment to give since any package can be 'avoided' simply by including its code and it isn't clear what is achieved by encouraging people to avoid the use of packages anyway.– cfrCommented Jun 22, 2015 at 18:56
fancyhdr
, for example, or are you talking about the chapter and section titles in the document body?