10

I am a novice for LaTeX, so need you guy's help.

I am wondering how to change section style in order to put the section number into the right side of section title in LaTeX, especially memoir class. So, I want it to show like [chapter number] section title [section number]

For example, let a chapter has three sections like section one, section two, and section three. Then, they should appear like

1. section one 1.
1. section two 2.
1. section three 3.

Can anybody do that for me?

2
  • That is not possible without major reprogramming
    – daleif
    Commented Aug 5, 2011 at 12:06
  • Welcome to TeX.sx! Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question.
    – N.N.
    Commented Aug 9, 2011 at 8:48

3 Answers 3

8
+50

In case you do not want the section number to the right of the section title in the table of contents (see BernS' answer), you could use the titlesec package to redefine the section header layout:

\documentclass{memoir}

\usepackage{lipsum}

\usepackage[explicit]{titlesec}
% \titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before>}[<after>]
\titleformat{\section}[hang]{\normalfont\Large\bfseries}%
    {\thechapter.}{4pt}%
    {#1 \arabic{section}.}

\begin{document}
\tableofcontents
\chapter{Chapter}
\section{Section one}
\lipsum[1]
\section{Section two}
\lipsum[2]
\section{Section three}
\lipsum[3]
\end{document}

Credits go to Raphink's answer to my question regarding a problem I had with reading documentation properly. Consider voting his answer up. ;-)

2
  • While I don't know what the OP intended exactly, this seems to be the easiest solution (and most useful one to me personally, because it cleared a misunderstanding I had about titlesec's formatting). Hence the (token) bounty.
    – Caramdir
    Commented Aug 13, 2011 at 19:17
  • @Caramdir: /me takes a humble bow. Being able to give something back to your community here (which has helped me much on my first venture into LaTeX) is a good feeling.
    – DevSolar
    Commented Aug 14, 2011 at 8:15
4

It is possible to manually redefine the \section (and \section*, assuming that this even makes sense) command to allow for this. For the moment

  • \section{<sec title>} produces <chapnum> \quad <sec title> \quad <secnum>; and
  • \section*{<sec title>} produces <chapnum> \quad <sec title>.

The former produces the usual entry in the table of contents since the manual redefinition of \section{...} does not include a redefinition of \thesection. Also, references made to sections will be referenced as <chapnum>.<secnum> (or, more specifically, \thechapter.\arabic{section}) by default. However, this behaviour as well as that of the sectioning command, can be changed if needed.

The basic redefinition was taken from Vincent Zoonekynd's LaTeX page on sections. Here's a minimal example:

\documentclass{memoir}
\usepackage[margin=15mm]{geometry}% Page layout
\usepackage{lipsum}% Dummy text

% Taken from http://zoonek.free.fr/LaTeX/LaTeX_samples_section/0.html
\makeatletter
\def\section{\@ifstar\unnumberedsection\numberedsection}
\def\numberedsection{\@ifnextchar[%]
  \numberedsectionwithtwoarguments\numberedsectionwithoneargument}
\def\unnumberedsection{\@ifnextchar[%]
  \unnumberedsectionwithtwoarguments\unnumberedsectionwithoneargument}
\def\numberedsectionwithoneargument#1{\numberedsectionwithtwoarguments[#1]{#1}}
\def\unnumberedsectionwithoneargument#1{\unnumberedsectionwithtwoarguments[#1]{#1}}
\def\numberedsectionwithtwoarguments[#1]#2{%
  \ifhmode\par\fi
  \removelastskip
  \vskip 3ex\goodbreak
  \refstepcounter{section}%
  \noindent
  \begingroup
  \leavevmode\Large\bfseries\raggedright
  \thechapter\quad 
  #2
  \quad\arabic{section}
  \par
  \endgroup
  \vskip 2ex\nobreak
  \addcontentsline{toc}{section}{%
    \protect\numberline{\thesection}% ToC entry
    #1}%
  }
\def\unnumberedsectionwithtwoarguments[#1]#2{%
  \ifhmode\par\fi
  \removelastskip
  \vskip 3ex\goodbreak
%  \refstepcounter{section}%
  \noindent
  \begingroup
  \leavevmode\Large\bfseries\raggedright
  \leavevmode\Large\bfseries\raggedright
  \thechapter\quad 
  #2
%  \quad\arabic{section}
  \par
  \endgroup
  \vskip 2ex\nobreak
  \addcontentsline{toc}{section}{%
%    \protect\numberline{\thesection}% ToC entry
    #1}%
}
\makeatother

\begin{document}
\chapter{First chapter} \lipsum[1]
\section{First section} \lipsum[2]
\section{Second section} \lipsum[3]
\section{Third section} \lipsum[4]
\section{Final section} \lipsum[5]
\end{document}

...and the associated output:

Manual section style

I used the geometry package merely for layout purposes (so the MWE fits on one page) and the lipsum package for dummy text.

2

The following should look like what you'd want to have (as described in the text, so "[chapter]. Title [section]."):

\documentclass{memoir}
\usepackage{lipsum}
\usepackage[utf8x]{inputenc}

\newcommand\secstore{}
\newcommand\mythesection{\arabic{chapter}.}

\newcommand\mysection[1]{%
    \let\secstore\thesection%
    \let\thesection\mythesection%
    \section{#1 \arabic{section}.}%
    \let\thesection\secstore%
}%


\begin{document}
    \chapter{Test}

    \mysection{This is section 1}
    \lipsum[1]

    \mysection{This is section 2}
    \lipsum[2]

    \mysection{This is section 3}
    \lipsum[3]
\end{document}

If you want it the same way as in your sample "[section]. Title [section]." then the following command should be changed above:

\newcommand\mythesection{\arabic{section}.}

Anyway, it's also possible to save and overload \section so that it is not necessary to use \mysection.

And here is the output provided by the solution above:

Output

4
  • I have two comments. First, there should be no need to \renewcommand at each of the macro invocations. Second, * is not a part of control sequence names; thus your second \let lets \thesection to the character * and then \secstore is expanded. Commented Aug 9, 2011 at 9:18
  • @Andrey Vihrov I changed the code above according your comments.
    – BernS
    Commented Aug 9, 2011 at 9:27
  • This puts the section number to the right of the section title in the table of contents, too.
    – DevSolar
    Commented Aug 9, 2011 at 11:27
  • @DevSolar, I hope this was intended by the question creator too...
    – BernS
    Commented Aug 9, 2011 at 11:45

You must log in to answer this question.