6

I am newbie with latex and I try to create a section with movable section number tabs as shown below:

enter image description here

In the following code, I used both tikz and titlesec packages to format the required sections

\documentclass{report}
\usepackage{totcount}
\usepackage{tikz}
\usepackage{blindtext}
    \usetikzlibrary{calc,shapes,arrows,positioning,patterns,fit,decorations.pathmorphing}
    \usetikzlibrary{decorations.text}
\usepackage[explicit,calcwidth,compact]{titlesec}
\regtotcounter{section}
\newcommand{\secnum}{\arabic{section}}
\titleformat{\section}{\Large\bfseries}{}{0mm}{\tikz[]{
\node [rectangle,,inner sep=0cm,text width=\textwidth] (A) {#1};
\pgfmathsetmacro\ABC{(\secnum-1)/(\totvalue{section}-1)}
\node [yshift=0.4cm] (B) at ($([xshift=1cm]A.north west)!{\ABC}!([xshift=-1cm]A.north east)$) {\thesection};
\draw [rounded corners] ([yshift=0.2cm]A.north west) -| (B.north west) -- (B.north east) |- ([yshift=0.2cm]A.north east);
\draw ([yshift=-0.2cm]A.south west) -- ([yshift=-0.2cm]A.south east);
}}

\begin{document}
\chapter{First Chapter}
\section{First section}
A short paragraph for first section
\section{Second section}
The second paragraph
\section{Third section}
The third paragraph
\section{Last section}
The last paragraph

\chapter{Second Chapter}
\section{First section}
A short paragraph for first section
\section{Last section}
The Last paragraph

\end{document}

The problem is If I use totcount with section counter, then It counts only the sections for the last chapter only (It counts 2 for my code). Is there any macro that counts the sections for each chapter? sorry for my simple English.

3

This might not be the easiest way, but basically, you may use a new auxiliary file which stores this per chapter. Please note that this requires two compilation runs to work (and the \flushchapter before \end{document).

section style

\documentclass{report}
\usepackage{tikz}
\usepackage{blindtext}
    \usetikzlibrary{calc,shapes,arrows,positioning,patterns,fit,decorations.pathmorphing}
    \usetikzlibrary{decorations.text}
\usepackage[explicit,calcwidth,compact]{titlesec}

\usepackage{etoolbox}
\makeatletter
\IfFileExists{\jobname.chap}{\input{\jobname.chap}}{}

\newwrite\chapfile
\immediate\openout\chapfile=\jobname.chap

\let\origchapter\chapter
\def\flushchapter{\protected@write\chapfile{}{\string\expandafter\string\def\string\csname\space chapter\the\c@chapter\string\endcsname{\the\c@section}}}
\renewcommand\chapter{\flushchapter\origchapter}

\titleformat{\section}{\Large\bfseries}{}{0mm}{\tikz[]{
\node [rectangle,,inner sep=0cm,text width=\textwidth] (A) {#1};
\ifcsdef{chapter\the\c@chapter}{%
    \pgfmathsetmacro\ABC{(\the\c@section-1)/(\csname chapter\the\c@chapter\endcsname - 1)}%
}{%
    \pgfmathsetmacro\ABC{1}%
}
\node [yshift=0.4cm] (B) at ($([xshift=1cm]A.north west)!{\ABC}!([xshift=-1cm]A.north east)$) {\thesection};
\draw [rounded corners] ([yshift=0.2cm]A.north west) -| (B.north west) -- (B.north east) |- ([yshift=0.2cm]A.north east);
\draw ([yshift=-0.2cm]A.south west) -- ([yshift=-0.2cm]A.south east);
}}
\makeatother

\begin{document}
\chapter{First Chapter}
\section{First section}
A short paragraph for first section
\section{Second section}
The second paragraph
\section{Third section}
The third paragraph
\section{Last section}
The last paragraph

\chapter{Second Chapter}
\section{First section}
A short paragraph for first section
\section{Last section}
The Last paragraph

\flushchapter
\end{document}
  • I already expected an easier way without using internal macros as a newbie, but it works anyway. – Muhammed Hashim Sep 11 '18 at 7:14
  • 1
    @MuhammedHashim That's just what I would come up with. Maybe somebody else has an easier way. – TeXnician Sep 11 '18 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.