0

I want to align the texts after the numbers (6, 6.1, 6.1.1) in (Chapter, Section, Subsection) titles in one page. But I can only find the alignments of the Numbers for example left aligned.

I have now the new problem. It works for the headings of Chapter, Section and Subsection and also Subsubsection. The heading of Subsubsection does not have numbes. How to make the text of Subsubsection align with the number of Chapter or Section or Subsection?

\documentclass
  [openright,listof=totoc,bibliography=totoc,twoside,fontsize=12pt,
   english,parskip=half,headinclude,footinclude=false,headsepline,
   DIV17,BCOR16mm,numbers=noenddot,2.1headlines,appendixprefix,
   cleardoublepage=empty
  ]{scrbook}
5
  • 1
    Note that some of the options are deprecated and should be replaced: DIV17DIV=17, BCOR16mmBCOR=16mm, 2.1headlinesheadline=2.1. See the warnings you get for more information. Mar 7, 2017 at 9:49
  • 1
    Please always post a MWE instead of a code snippet. It would help us to see the problem, compare it with our own problems, and to test our suggestions. Mar 7, 2017 at 9:51
  • sorry about that, I am new here. How can I post a MWE instead of a copy past code? What is the abbreviation of MWE ? I will try to do this in the future.
    – Peter Yang
    Mar 7, 2017 at 10:17
  • 1
    Instead of adding a new question into the existing one, you should add a MWE to this question and ask a following question with the new question. In the following question you should also specify whether the alignment should really be per page (as asked here) or per document (as in the accepted answer). Mar 12, 2017 at 10:47

2 Answers 2

2

Here is a suggestion that aligns the texts only for the whole document (and not per page). And you have to set the space needed for the section numbers manually.

\documentclass{scrbook}[2015/10/03]
\newdimen\sectionnumberwdmax
\setlength\sectionnumberwdmax{2cm}% <- space needed for the section numbers
%\renewcommand\raggedsection{\raggedleft}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \@hangfrom%
    {\makebox[\sectionnumberwdmax][l]{\hskip#2#3}}%
    {\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #4}}%
}
\renewcommand\chapterlinesformat[3]{%
  \@hangfrom%
  {\makebox[\sectionnumberwdmax][l]{#2}}%
  {\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #3}}%
}
\makeatother

\begin{document}
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\end{document}

enter image description here


If unnumbered titles should be aligned with the numbers of the other titles:

\documentclass{scrbook}[2015/10/03]
\newdimen\sectionnumberwdmax
\setlength\sectionnumberwdmax{2cm}% <- space needed for the section numbers
%\renewcommand\raggedsection{\raggedleft}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#3}{}
    {\@hangfrom{\hskip#2#3}{#4}}
    {\@hangfrom%
      {\makebox[\sectionnumberwdmax][l]{\hskip#2#3}}%
      {\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #4}}}%
}
\renewcommand\chapterlinesformat[3]{%
    \ifstr{#2}{}
    {\@hangfrom{#2}{#3}}
    {\@hangfrom%
      {\makebox[\sectionnumberwdmax][l]{#2}}%
      {\parbox[t]{\dimexpr\textwidth-\sectionnumberwdmax\relax}{\raggedsection #3}}}%
}
\makeatother

\begin{document}
\tableofcontents
\chapter{A chapter}
\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}
\end{document}

enter image description here

4
  • Thanks a lot for your help, esdd. I tried your codes into my latex file. But I get an error like this ! LaTeX Error: \chapterlinesformat undefined. I can not find even find this error report in google.
    – Peter Yang
    Mar 7, 2017 at 9:54
  • Which KOMA-Script version do you use? The command \chapterlinesformat was introduced in version 3.19 in autumn 2015. Current version is 3.21
    – esdd
    Mar 7, 2017 at 10:06
  • My Miktex version is 2.9. It was installed in 2003. Is this the problem due to the old version ? Is it possible to update the version 2.9 to the newest version or just update the packages? Sorry about so many questions. I just know some basic application of latex during my scientific work.
    – Peter Yang
    Mar 7, 2017 at 10:20
1

This works per page, but needs at least two LaTeX runs after adding a new heading:

\documentclass{scrbook}
\makeatletter
\AtBeginDocument{%
  \if@filesw
    \write\@auxout{%
      \noexpand\providecommand*\noexpand\minnumwidth[3]{}%
    }%
  \fi
}
\newcommand*{\minnumwidth}[3]{%
  \scr@ifundefinedorrelax{maxnumwidth@page@#2}{%
    \expandafter\gdef\csname maxnumwidth@page@#2\endcsname{#3}%
  }{%
    \expandafter\ifdim \csname maxnumwidth@page@#2\endcsname<#3\relax
      \expandafter\gdef\csname maxnumwidth@page@#2\endcsname{#3}%
    \fi
  }%
  \expandafter\gdef\csname pageof@#1\endcsname{#2}%
}
\newcommand*{\makenumberboxof}[1]{%
  \scr@ifundefinedorrelax{pageof@#1}{%
    #1\autodot\enskip
  }{%
    \scr@ifundefinedorrelax{maxnumwidth@page@\@nameuse{pageof@#1}}{%
      #1\autodot\enskip
    }{%
      \makebox[\@nameuse{maxnumwidth@page@\@nameuse{pageof@#1}}][l]%
      {#1\autodot\enskip}%
    }%
  }%
}
\newcommand*{\changenumformat}[1]{%
  \expandafter\renewcommand\expandafter*\csname #1format\endcsname{%
    \begingroup
      \settowidth{\@tempdima}{\csname the#1\endcsname\autodot\enskip}%
      \edef\reserved@a{\string\minnumwidth{\csname
          the#1\endcsname}{\noexpand\thepage}{\the\@tempdima}}%
      \if@filesw
        \expandafter\write\expandafter\@auxout\expandafter{\reserved@a}%
      \fi
    \endgroup
    \makenumberboxof{\csname the#1\endcsname}%
  }%
}
\makeatother
\changenumformat{chapter}
\changenumformat{section}
\changenumformat{subsection}
\changenumformat{subsubsection}
\makeatother

\usepackage{blindtext}

\begin{document}
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\blinddocument\blinddocument\blinddocument\blinddocument\blinddocument
\chapter{A chapter}
\section{A section}
\subsection{A subsection}

\appendix
\chapter{An appendix}
\section{An appendix section}
\subsection{An appendix subsection}
\end{document}

After the first LaTeX run the headings of the last page are:

enter image description here

but after the third run they are:

enter image description here

Note that it does work only as long as the numbers of chapters, sections and subsections are expandable.

Not numbered headings are still left aligned. So, if I understand your addendum to the question correct, nothing has to be done for those. But if I don't and if this per page solution should also be used for not numbered headings, you have to add additional code, e.g., for the \subsubsection:

\usepackage{xpatch}
\newcounter{notnumberedsection}
\renewcommand*{\thenotnumberedsection}{\arabic{notnumberedsection}*}
\makeatletter
\xpretocmd{\subsubsection}{%
  \let\scr@subsubsection@sectionindent\z@
  \ifnumbered{subsubsection}{%
    \typeout{subsubsection numbered!}%
  }{%
    \stepcounter{notnumberedsection}%
    \if@filesw
      \edef\reserved@a{\string\minnumwidth{\thenotnumberedsection}{\noexpand\thepage}{\string\z@}}%
      \show\reserved@a
      \expandafter\write\expandafter\@auxout\expandafter{\reserved@a}%
    \fi
    \scr@ifundefinedorrelax{pageof@\thenotnumberedsection}{%
      \typeout{Don't know page of \thenotnumberedsection!}%
    }{%
      \scr@ifundefinedorrelax{maxnumwidth@page@\@nameuse{pageof@\thenotnumberedsection}}{%
        \typeout{Don't know max width for page \@nameuse{pageof@\thenotnumberedsection}!}%
      }{%
        \expandafter\let\expandafter\scr@subsubsection@sectionindent
        \csname
        maxnumwidth@page@\@nameuse{pageof@\thenotnumberedsection}\endcsname
        \show\scr@subsubsection@sectionindent
      }%
    }%
  }%
}{}{}

This would result in a last page:

enter image description here

but looks very strange in most cases, e.g., page 58 of the example:

enter image description here

If would recommend to not use this! If could be better to number the \subsubsection using

\setcounter{secnumdepth}{\subsubsectionnumdepth}
1
  • Danke Schweinebacke.
    – Peter Yang
    Mar 7, 2017 at 13:43

You must log in to answer this question.

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