2

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.

7
  • 1
    Do you have any compelling reason not to use packages? Using a package usually makes the code easier to write and understand, and also likely anticipates interactions with other things that you might not have thought about. It's not that you can't do this stuff without packages, but why would you want to?
    – Alan Munn
    Commented Jun 22, 2015 at 13:45
  • I am already using fanychdr, facy page package,listing and etc... The less the the dependencies the more marks I can score.
    – user80596
    Commented Jun 22, 2015 at 14:11
  • 1
    Who gives you marks depending on the number of packages? Beside this: You should put a bit more effort in your question. You even didn't mention the class you are using. Make a minimal example. Commented Jun 22, 2015 at 14:17
  • 1
    So is this a homework assignment?
    – Alan Munn
    Commented Jun 22, 2015 at 14:20
  • What exactly do you mean by heading? Is it the headers at the top of pages, as produced with the help of fancyhdr, for example, or are you talking about the chapter and section titles in the document body? Commented Jun 22, 2015 at 14:28

1 Answer 1

5

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}

enter image description here

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.

4
  • 1
    For 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.
    – Mico
    Commented Jun 22, 2015 at 15:20
  • @Mico True. I've updated the answer. Thanks. Commented Jun 22, 2015 at 15:26
  • 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.
    – cfr
    Commented 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.
    – cfr
    Commented Jun 22, 2015 at 18:56

You must log in to answer this question.

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