LaTeX forum ⇒ Text FormattingInclude chapter number in section number Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Toty
Posts: 10
Joined: Fri Apr 13, 2012 1:40 am

Include chapter number in section number

Postby Toty » Fri Apr 13, 2012 1:43 am

Hi,

I'd like to include the chapter number into my section numbers. Usually it is automatically done, but not in this Template that I'm working with, which doesn't offer me any support

The part of the template the controls the chapter/sections formatting goes:

  1. %%%%% CHAPTER HEADER %%%%
  2. \usepackage{color}
  3. \usepackage{tikz}
  4. \usepackage[explicit]{titlesec}
  5. \newcommand*\chapterlabel{}
  6. %\renewcommand{\thechapter}{\Roman{chapter}}
  7. \titleformat{\chapter}[display] % type (section,chapter,etc...) to vary, shape (eg display-type)
  8. {\normalfont\bfseries\Huge} % format of the chapter
  9. {\gdef\chapterlabel{\thechapter\ }} % the label
  10. {0pt} % separation between label and chapter-title
  11. {\begin{tikzpicture}[remember picture,overlay]
  12. \node[yshift=-8cm] at (current page.north west)
  13. {\begin{tikzpicture}[remember picture, overlay]
  14. \draw[fill=black] (0,0) rectangle(35.5mm,15mm);
  15. \node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
  16. {\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}}; %the black l is just to get better base-line alingement
  17. \node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
  18. {\parbox[top][30mm][t]{\textwidth}{\color{black}#1}};
  19. \end{tikzpicture}
  20. };
  21. \end{tikzpicture}
  22. \gdef\chapterlabel{}
  23. } % code before the title body
  24.  
  25. \titlespacing*{\chapter}{0pt}{50pt}{30pt}
  26. \titlespacing*{\section}{0pt}{13.2pt}{*0} % 13.2pt is line spacing for a text with 11pt font size
  27. \titlespacing*{\subsection}{0pt}{13.2pt}{*0}
  28. \titlespacing*{\subsubsection}{0pt}{13.2pt}{*0}
  29.  
  30. \newcounter{myparts}
  31. \newcommand*\partlabel{}
  32. \titleformat{\part}[display] % type (section,chapter,etc...) to vary, shape (eg display-type)
  33. {\normalfont\bfseries\Huge} % format of the part
  34. {\gdef\partlabel{\thepart\ }} % the label
  35. {0pt} % separation between label and part-title
  36. {\setlength{\unitlength}{20mm}
  37. \addtocounter{myparts}{1}
  38. \begin{tikzpicture}[remember picture,overlay]
  39. \node[anchor=north west,xshift=-65mm,yshift=-6.9cm-\value{myparts}*20mm] at (current page.north east) % for unknown reasons: 3mm missing -> 65 instead of 62
  40. {\begin{tikzpicture}[remember picture, overlay]
  41. \draw[fill=black] (0,0) rectangle(62mm,20mm); % -\value{myparts}\unitlength
  42. \node[anchor=north west,yshift=-6.1cm-\value{myparts}*20mm,xshift=-60.5mm,minimum height=30mm,inner sep=0mm] at (current page.north east)
  43. {\parbox[top][30mm][t]{55mm}{\raggedright \color{white}Part \partlabel $\phantom{\textrm{l}}$}}; %the phantom l is just to get better base-line alingement
  44. \node[anchor=north east,yshift=-6.1cm-\value{myparts}*20mm,xshift=-63.5mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north east)
  45. {\parbox[top][30mm][t]{\textwidth}{\raggedleft \color{black}#1}};
  46. \end{tikzpicture}
  47. };
  48. \end{tikzpicture}
  49. \gdef\partlabel{}
  50. } % code before the title body
  51.  


Does anyone knows how to solve this ? I've been visiting many pages and forums on the Net but can't find a way out.

Thanks

User avatar
Stefan Kottwitz
Site Admin
Posts: 9323
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Fri Apr 13, 2012 9:18 am

Hi Toty,

welcome to the board!

The macro \thesection prints the section number, so you could redefine it, such as

  1. \renewcommand{\thesection}{\thechapter.\arabic{section}}


Usually the section counter is automatically reset at the beginning of each chapter. Perhaps your template does ist already, if not and if it's required, let us know. In this case I would recommend to use chngcntr instead of the line above.

Stefan
Site admin

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Apr 13, 2012 9:06 pm

You need to give us more information, i.e. a Infominimal working example which shows the unwanted behaviour. I included your code in the example below and get sections numbered “1.1” and “2.1”, resp.

  1. \documentclass{book}
  2. \usepackage{lipsum}% for dummy text
  3. %%%%% CHAPTER HEADER %%%%
  4. % \usepackage{color}
  5. % 1.: better use `xcolor' verwenden. 2.: `tikz' already loads `xcolor'
  6. \usepackage{tikz}
  7. \usepackage[explicit]{titlesec}
  8. \newcommand*\chapterlabel{}
  9. %\renewcommand{\thechapter}{\Roman{chapter}}
  10. \titleformat{\chapter}[display] % type (section,chapter,etc...) to vary, shape (eg display-type)
  11. {\normalfont\bfseries\Huge} % format of the chapter
  12. {\gdef\chapterlabel{\thechapter\ }} % the label
  13. {0pt} % separation between label and chapter-title
  14. {\begin{tikzpicture}[remember picture,overlay]
  15. \node[yshift=-8cm] at (current page.north west)
  16. {\begin{tikzpicture}[remember picture, overlay]
  17. \draw[fill=black] (0,0) rectangle(35.5mm,15mm);
  18. \node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
  19. {\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}}; %the black l is just to get better base-line alingement
  20. \node[anchor=north west,yshift=-7.2cm,xshift=37mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
  21. {\parbox[top][30mm][t]{\textwidth}{\color{black}#1}};
  22. \end{tikzpicture}
  23. };
  24. \end{tikzpicture}
  25. \gdef\chapterlabel{}
  26. } % code before the title body
  27.  
  28. \titlespacing*{\chapter}{0pt}{50pt}{30pt}
  29. \titlespacing*{\section}{0pt}{13.2pt}{*0} % 13.2pt is line spacing for a text with 11pt font size
  30. \titlespacing*{\subsection}{0pt}{13.2pt}{*0}
  31. \titlespacing*{\subsubsection}{0pt}{13.2pt}{*0}
  32.  
  33. \newcounter{myparts}
  34. \newcommand*\partlabel{}
  35. \titleformat{\part}[display] % type (section,chapter,etc...) to vary, shape (eg display-type)
  36. {\normalfont\bfseries\Huge} % format of the part
  37. {\gdef\partlabel{\thepart\ }} % the label
  38. {0pt} % separation between label and part-title
  39. {\setlength{\unitlength}{20mm}
  40. \addtocounter{myparts}{1}
  41. \begin{tikzpicture}[remember picture,overlay]
  42. \node[anchor=north west,xshift=-65mm,yshift=-6.9cm-\value{myparts}*20mm] at (current page.north east) % for unknown reasons: 3mm missing -> 65 instead of 62
  43. {\begin{tikzpicture}[remember picture, overlay]
  44. \draw[fill=black] (0,0) rectangle(62mm,20mm); % -\value{myparts}\unitlength
  45. \node[anchor=north west,yshift=-6.1cm-\value{myparts}*20mm,xshift=-60.5mm,minimum height=30mm,inner sep=0mm] at (current page.north east)
  46. {\parbox[top][30mm][t]{55mm}{\raggedright \color{white}Part \partlabel $\phantom{\textrm{l}}$}}; %the phantom l is just to get better base-line alingement
  47. \node[anchor=north east,yshift=-6.1cm-\value{myparts}*20mm,xshift=-63.5mm,text width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north east)
  48. {\parbox[top][30mm][t]{\textwidth}{\raggedleft \color{black}#1}};
  49. \end{tikzpicture}
  50. };
  51. \end{tikzpicture}
  52. \gdef\partlabel{}
  53. } % code before the title body
  54. \begin{document}
  55. \chapter{One}
  56. \section{One}
  57. \lipsum[1]
  58.  
  59. \chapter{Two}
  60. \section{Two}
  61. \lipsum[2]
  62. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

Toty
Posts: 10
Joined: Fri Apr 13, 2012 1:40 am

Postby Toty » Sun Apr 15, 2012 3:16 pm

Thanks a lot guys, it works now !
all the best

RalfAmr
Posts: 1
Joined: Wed Dec 19, 2018 8:13 pm

Postby RalfAmr » Wed Dec 19, 2018 8:20 pm

  1. \numberwithin{section}{chapter}


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 3 guests