3

I have a file composed by many sections: I'm not sure of this choice, and maybe in the future I want to switch to chapters (or to subsections, for example).

My solution is the following command:

\newcommand{\mychapter}[1]
{
\section{#1}
}

And I use \mychapter{Title} instead of \section{Title}. In this way I can simply edit the command to switch from sections to whatever I want.

Is this a valid solution?

2
  • 1
    You are inserting spurious spaces -- they could matter, also you won't be able to use the optional arguments. Feb 26, 2016 at 11:18
  • 1
    As far as I understand your problem, you just need to replace \chapter with section and so on. This is the most correct and simple to do with editor capability "replace". You only need to start replacing at the lowest level of your document divisions.
    – Zarko
    Feb 26, 2016 at 11:37

4 Answers 4

4

If you really want to do this:

\begingroup
\let\orisection\section
\let\orisubsection\subsection
\let\orisubsubsection\subsubsection
\let\oriparagraph\paragraph
\let\orisubparagraph\subparagraph
\global\let\section\chapter
\global\let\subsection\orisection
\global\let\subsubsection\orisubsection
\global\let\paragraph\orisubsubsection
\global\let\subparagraph\oriparagraph
\global\let\subsubparagraph\orisubparagraph % for symmetry
\endgroup
2

I would find this rather confusing if I went back to a document after a while, or was asked to review a document for someone else. You could use something like \newcommand{\MyLevelOneDiv}[1]{\section{#1}}, but I would take a different approach.

In any decent text editor you can do this with a simple find/replace with regular expressions. By searching for \\section\{([^}]*)\} and replacing with \\subsection{$1} you can easily demote all sections to subsections. This simple version doesn't work for optionl short titles, or if you have macros with } inside the title.

A version that would work for (sub)section names such as \section[short title]{A rather \emph{long} title} is to replace \\section(\[[^\]]*\])?(\{.*\})\s*$ with \\subsection$1\$2 (although this assumes that you have nothing other than whitespace after the closing } -- no good if you want a comment there.

The exact syntax may need tweaking depending on your regex engine, especially in terms of exactly which brakcets need escaping with a \ in the search, and how to refer to the captured groups in the replacement. My versions are tested under jEdit.

Many TeX-specific editors don't offer this feature, which is why I don't use them, preferring a general programming text editor.

2

You could use the memoir class which encompasses the book, report and article classes. Set up your document as though it was a book or report (i.e. use \chapter as the main document division). Then if you specify

\documentclass[...]{memoir}

it will be set like a book or report, but if you specify

\documentclass[article,...]{memoir}

it will be set like an article with chapters set as section, sections set as subsections, etc.

1

It should do the job, but you should take care of subsections, subsubsections, ... as well.

\newcommand{\mychapter}[1]
{
    \section{#1}
}
\newcommand{\mysection}[1]
{
    \subsection{#1}
}
\newcommand{\mysubsection}[1]
{
    \subsubsection{#1}
}
...

This way you can adjust your whole structure in a single place.

You must log in to answer this question.

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