3

I just discovered the enumerate package, what excited me because some time ago I was wondering about something similar (options where a stands for \alph{<counter>}, A for \Alph{<counter>}, i for \roman{<counter>}, etc.) but for the sectionning of the whole document. That is, to copy the notation of teacher while transcripting my history lessons, use something like \usepackage[I/1/I/A/1/a]{sectioning} (with maybe some additionnal syntax to specify when we want each counter to be reset to 0) instead of always writing

\renewcommand{\thepart}{Tema \Roman{part}}
\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\thesection .\Alph{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection .\arabic{subsubsection}}
\renewcommand{\theparagraph}{\thesubsubsection .\alph{paragraph}}

I formerly abandoned that idea because at time I didn’t know how to write a macro for a such thing (especially the a-to-\alph{<actual-counter>}-like part), but now not only I’m asking myself if a such package doesn’t already exist, but also if otherwise there aren’t already packages that could help me try to write one like that, as enumerate package do for enumerate environment (I’m also wondering if the same could be applied to enumerate and sub-enumerate environments).

So in brief, my question is: are there packages doing that or something like, and otherwise are there packages which could help me to achieve that or something like?

1
  • If you find the package enumerate sophisticated, then have a look at enumitem ;-) (No, I am not the author of enumitem ;-))
    – user31729
    Feb 26 '16 at 23:01
5

As far as I know there is no such package but perhaps, this code will lead to a package (given I have some time to improve the code and write the package)

Updated version

Syntax:

  • I etc. uses just the counter formatter as requested by the O.P.
  • I+ etc. prepends the previous \the... formatter, e.g. \thesubsection will be used then as \thesection \Roman{subsection}
  • I+. will add a dot before \Roman{...}.

It's not optimal so far, but working


\documentclass{book}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \l_galex_default_seq
\newcommand{\filldefaultseq}{%
  \seq_set_split:Nnn \l_galex_default_seq {/} {I+/A/I/A/1/a/i}
}

\seq_set_from_clist:Nn \l_sectionname_seq {part,chapter,section,subsection,subsubsection,paragraph,subparagraph}

\tl_gset:Nn \g_prepart_name {\space}
\tl_gset:Nn \g_prechapter_name {\thepart}
\tl_gset:Nn \g_presection_name {\thechapter}
\tl_gset:Nn \g_presubsection_name {\thesection}
\tl_gset:Nn \g_presubsubsection_name {\thesubsection}
\tl_gset:Nn \g_preparagraph_name {\thesubsubsection}
\tl_gset:Nn \g_presubparagraph_name {\theparagraph}

\tl_gset:Nn \g_part_structurename {Tema}
\tl_gset:Nn \g_chapter_structurename {}
\tl_gset:Nn \g_section_structurename {}
\tl_gset:Nn \g_subsection_structurename {}
\tl_gset:Nn \g_subsubsection_structurename {}
\tl_gset:Nn \g_paragraph_structurename {}
\tl_gset:Nn \g_subparagraph_structurename {}


\NewDocumentCommand{\ChangeCounterFormats}{o}{%
  \IfValueTF{#1}{%
    \seq_set_split:Nnn \l_tmpa_seq {/} {#1}
  }{
    \seq_set_eq:NN \l_tmpa_seq \l_galex_default_seq
  }
  \int_zero:N \l_tmpa_int
  \seq_map_inline:Nn \l_sectionname_seq {%
    \int_incr:N \l_tmpa_int
    \str_case_x:nn {\seq_item:Nn \l_tmpa_seq {\l_tmpa_int}}{%
      {I+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\Roman{##1}}}
      {i+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\roman{##1}}}
      {A+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\Alph{##1}}}
      {a+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\alph{##1}}}
      {1+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\arabic{##1}}}
      {I+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \Roman{##1}}}
      {i+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \roman{##1}}}
      {A+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \Alph{##1}}}
      {a+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \alph{##1}}}
      {1+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \arabic{##1}}}
      {I}{\expandafter\renewcommand\csname the##1\endcsname{\Roman{##1}}}
      {i}{\expandafter\renewcommand\csname the##1\endcsname{\roman{##1}}}
      {a}{\expandafter\renewcommand\csname the##1\endcsname{\alph{##1}}}
      {A}{\expandafter\renewcommand\csname the##1\endcsname{\Alph{##1}}}
      {1}{\expandafter\renewcommand\csname the##1\endcsname{\arabic{##1}}}
    }
  }
}
\ExplSyntaxOff

\setcounter{secnumdepth}{6}

\setcounter{tocdepth}{6}
\begin{document}
\tableofcontents

\filldefaultseq

\ChangeCounterFormats[I+/A/1+./i+./1+./i+./a+.]

\part{Foo}

\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\paragraph{First paragraph}
\subparagraph{First subparagraph}



\part{Foo}

\chapter{Second chapter}
\section{Second section}
\subsection{Second subsection}
\subsubsection{Second subsubsection}
\paragraph{Second paragraph}
\subparagraph{Second subparagraph}


\chapter{Third chapter}
\section{Third section}
\subsection{Third subsection}
\subsubsection{Third subsubsection}
\paragraph{Third paragraph}
\subparagraph{Third subparagraph}

\end{document}

enter image description here

Update 2016/02/27

I created a secfmt.sty to change the counter formatting styles and a driver file.

Some macro names have been changed and I added \StoreOriginalCounterFormats and \RestoreOriginalCounterFormats macros which store \thepart etc. and restore them (if needed)

secfmt.sty

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{secfmt}

\RequirePackage{xparse}

\ExplSyntaxOn

% Data section 
\seq_new:N \g_secfmt_default_seq
\seq_gset_from_clist:Nn \g_secfmt_sectionname_seq {part,chapter,section,subsection,subsubsection,paragraph,subparagraph}


\@ifclassloaded{memoir}{%
  \seq_gput_left:Nn \g_secfmt_sectionname_seq {book}
  \seq_gput_left:Nn \g_secfmt_default_seq {A}
}{}


\tl_gset:Nn \g_prebook_name {} % Only relevant for `memoir.cls`

\@ifclassloaded{memoir}{%
  \tl_gset:Nn \g_prepart_name {\thebook}
}{
  \tl_gset:Nn \g_prepart_name {\space}
}

\tl_gset:Nn \g_prechapter_name {\thepart}
\tl_gset:Nn \g_presection_name {\thechapter}
\tl_gset:Nn \g_presubsection_name {\thesection}
\tl_gset:Nn \g_presubsubsection_name {\thesubsection}
\tl_gset:Nn \g_preparagraph_name {\thesubsubsection}
\tl_gset:Nn \g_presubparagraph_name {\theparagraph}

\tl_gset:Nn \g_book_structurename {Book}
\tl_gset:Nn \g_part_structurename {Tema}
\tl_gset:Nn \g_chapter_structurename {}
\tl_gset:Nn \g_section_structurename {}
\tl_gset:Nn \g_subsection_structurename {}
\tl_gset:Nn \g_subsubsection_structurename {}
\tl_gset:Nn \g_paragraph_structurename {}
\tl_gset:Nn \g_subparagraph_structurename {}

%% Function section

\NewDocumentCommand{\StoreOriginalCounterFormats}{}{%
  \seq_map_inline:Nn \g_secfmt_sectionname_seq {%
    \cs_gset_eq:cc {cs_orig_the##1} {the##1}
  }
}

\NewDocumentCommand{\RestoreOriginalCounterFormats}{}{%
  \seq_map_inline:Nn \g_secfmt_sectionname_seq {%
    \cs_gset_eq:cc {the##1} {cs_orig_the##1} 
  }
}

\AtBeginDocument{\StoreOriginalCounterFormats}

\NewDocumentCommand{\CreateDefaultCounterFormats}{o}{%
  \IfValueTF{#1}{%
    \seq_set_split:Nnn \g_secfmt_default_seq {/} {#1}
  }{%
    \seq_set_split:Nnn \g_secfmt_default_seq {/} {I+/A/I/A/1/a/i}
  }
}


\NewDocumentCommand{\ChangeCounterFormats}{o}{%
  \IfValueTF{#1}{%
    \seq_set_split:Nnn \l_tmpa_seq {/} {#1}
  }{
    \seq_set_eq:NN \l_tmpa_seq \g_secfmt_default_seq
  }
  \int_zero:N \l_tmpa_int % dummy counter
  \seq_map_inline:Nn \g_secfmt_sectionname_seq {%
    \int_incr:N \l_tmpa_int
    \str_case_x:nn {\seq_item:Nn \l_tmpa_seq {\l_tmpa_int}}{%
      {I+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\Roman{##1}}}
      {i+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\roman{##1}}}
      {A+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\Alph{##1}}}
      {a+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\alph{##1}}}
      {1+.}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name}.\arabic{##1}}}
      {I+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \Roman{##1}}}
      {i+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \roman{##1}}}
      {A+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \Alph{##1}}}
      {a+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \alph{##1}}}
      {1+}{\expandafter\renewcommand\csname the##1\endcsname{\tl_use:c {g_##1_structurename}\tl_use:c {g_pre##1_name} \arabic{##1}}}
      {I}{\expandafter\renewcommand\csname the##1\endcsname{\Roman{##1}}}
      {i}{\expandafter\renewcommand\csname the##1\endcsname{\roman{##1}}}
      {a}{\expandafter\renewcommand\csname the##1\endcsname{\alph{##1}}}
      {A}{\expandafter\renewcommand\csname the##1\endcsname{\Alph{##1}}}
      {1}{\expandafter\renewcommand\csname the##1\endcsname{\arabic{##1}}}
    }
  }
}
\ExplSyntaxOff


\endinput

driver.tex

\documentclass{book}

\usepackage{secfmt}

\CreateDefaultCounterFormats%

\setcounter{secnumdepth}{6}

\setcounter{tocdepth}{6}
\begin{document}
\tableofcontents


\ChangeCounterFormats[I+/A/1+./i+./1+./i+./a+.]

\part{First Part}

\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\paragraph{First paragraph}
\subparagraph{First subparagraph}



\part{Second part}

\chapter{Second chapter}
\section{Second section}
\subsection{Second subsection}
\subsubsection{Second subsubsection}
\paragraph{Second paragraph}
\subparagraph{Second subparagraph}


\chapter{Third chapter}
\section{Third section}
\subsection{Third subsection}
\subsubsection{Third subsubsection}
\paragraph{Third paragraph}
\subparagraph{Third subparagraph}

\RestoreOriginalCounterFormats

\part{Third part}

\chapter{Third chapter}
\section{Third section}
\subsection{Third subsection}
\subsubsection{Third subsubsection}
\paragraph{Third paragraph}
\subparagraph{Third subparagraph}


\chapter{Fourth chapter}
\section{Fourth section}
\subsection{Fourth subsection}
\subsubsection{Fourth subsubsection}
\paragraph{Fourth paragraph}
\subparagraph{Fourth subparagraph}


\end{document}
10
  • 1
    @galex-713: I don't understand. I showed the ToC in the image above -- it has the desired effect. Do you have the correct tocdepth setting? xparse is optimal, since it uses the new LaTeX 3 features, which simplies a lot of the code. Please do not stick to enumerate -- that's a totally different setup
    – user31729
    Feb 26 '16 at 23:57
  • 1
    For the ToC i tried it and I thought some change didn’t have effect, but after your comment I tried again and it works, so I probably forgot to compile once more ^^" For xparse I said it because it seemed to require really verbose code, and I supposed that since xparse is probably written from some LaTeX, written from some TeX, it would be possible to achieve the same thing with less code… otherwise why enumerate isn’t using it? For the LaTeX3 features, does it means LaTeX3 adds primitives that aren’t doable as well with TeX and aren’t used within enumerate?
    – galex-713
    Feb 27 '16 at 0:15
  • 2
    @galex-713 -- Actually the code for enumerate in latex.ltx is quite complicated. But enumerate is a list, which is very different (in the *TeX world) from code for sectioning. If they were the same thing, then a LaTeX document would only be a giant list, which is untrue. What Christian has done here is given you a whole new set of code that gives you the convenient \ChangeCounterFormats macro. (And he can't put it behind the scenes the way it is in latex.ltx or, for lists, in enumitem.sty.) Finally, LaTeX3 does not 'add' primitives in the true sense of the word.
    – jon
    Feb 27 '16 at 2:44
  • 1
    @galex-713 -- Of course, you can put the relevant code in a separate file called (say) mypackage.sty and then simply load it with \usepackage{mypackage}.
    – jon
    Feb 27 '16 at 2:45
  • 1
    @galex-713: I provided the 'package' secnum.sty and a driver file.
    – user31729
    Feb 27 '16 at 17:57

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.