2

This has been asked several times.

I thought that I had solved this out in the past,
but I cannot get it to work.

What I want:

I am seeking a solution which:

  1. Typesets the complete section number (ex: A.1.3 — 10) in all float counters,
    and any other counters which the author wishes to use.

  2. Provides the option of automatically choosing the current section depth.

  3. Permits the author to set the options in the preamble as well as in the document.

  4. Permits the author to alter the options mid-document if they have already been set.

What I'm Getting:

The \renewcommand in the preamble is not resetting the counter syntax,
as is evident in the equation counter numbering.

Also:

Beyond that, I do not belive that \determineSection works as intended.
I believe that it provides a counter value, rather than the coded name \thesection, etc.

How do I use the if switch in \determineSection to output code such as:

  • subsubsection
  • \thesubsection

Anything to do with csname?

MWE

Subfiles/Preamble.tex:

% meta tools
\usepackage{etoolbox}                   % allows if/else statements in code. [required for: standalone+nocite fix, equation numbering.]

% math
\usepackage{mathtools}                  % includes amsmath, supplements it.

% font
\usepackage{listings}                   % improved version of verbatim. imports script languages (with syntax coloring).

\usepackage{color}                      % color commands.
\usepackage[dvipsnames]{xcolor}         % additional color commands.

% layout: page/spacing/headings
\usepackage{appendix}                   % appendices.
\usepackage{scrlayer-scrpage}           % improved header commands. [supercedes `fancyhdr' package].

% references
\usepackage{tocloft}                    % allows generation of list of equations. (used in MWE only.)
\usepackage{chngcntr}                   % allows changes mid-document to section depth of equation counter reset.

% floats: figures/tables/lists




% Sectioned Counters:

% Save a copy of the original float counters.
\let\xtheequationOriginal\theequation
\let\xthefigureOriginal\thefigure
\let\xthetableOriginal\thetable
\let\xthelstlistingOriginal\thelstlisting
% Create blank commands which will later act as variable rich-format counters.
\newcommand{\sectionedEquation}{.}
\newcommand{\sectionedFigure  }{.}
\newcommand{\sectionedTable   }{.}
\newcommand{\sectionedListing }{.}
% Set the float counters to the rich-format counter commands.
\let\theequation\sectionedEquation
\let\thefigure\sectionedFigure
\let\thetable\sectionedTable
\let\thelstlisting\sectionedListing

% Command: Determine Section
\newcommand{\determineSection}{%  [Provides full section counter of current section, independent of the section depth.]
  \ifnum\value{subsubsection} > 0
  \ifnum\value{paragraph}     > 0 
  \ifnum\value{subparagraph}  > 0 \theparagraph
  \else                           \thesubsubsection  \fi
  \else                           \thesubsection     \fi
  \else                           \thesection        \fi
}

% Command: Sectioned Counter
\newcommand{\sectionedCounterStyle}[2]{#2--\arabic{#1}} % Input #1: equation, lstlisting, table, figure
                                                        % Input #2: \determineSection, \thesection, \thesubsection, ..

\newcommand{\sectionedCounter}     [2]                  % Input #1: \determineSection, \thesection, \thesubsection, ..
{                                                       % Input #2:           section,     section,     subsection, ..
  \counterwithin*{equation}  {#2} % Reset counter whenever there is a new \section
  \counterwithin*{figure}    {#2}
  \counterwithin*{table}     {#2}
  \counterwithin*{lstlisting}{#2} % The listings counter is not actually defined until \AtBeginDocument. 
                                  % Thus, if using this command (including listings) within the preamble, 
                                  %   use ``\AtBeginDocument{\sectionedCounter{<input>}}'' instead.

  \renewcommand{\sectionedEquation}{\sectionedCounterStyle{equation}  {#1}}
  \renewcommand{\sectionedFigure  }{\sectionedCounterStyle{figure}    {#1}}
  \renewcommand{\sectionedTable   }{\sectionedCounterStyle{table}     {#1}}
  \renewcommand{\sectionedListing }{\sectionedCounterStyle{lstlisting}{#1}}  
}

% Set float counters to include their full section number.
\AtBeginDocument{\sectionedCounter{\thesubsection}{subsection}}
% -\determineSection is chosen by default as a catch-all, such that
%   float counters adapt to any section level when running standalone subfiles
%   which do not contain a \sectionedCounter command.
%   It is assumed that in most cases, the author will set the counter depth to a specific level.




% Settings for importing scripts [listings package]
\lstset{frame = single}




% Settings for creating Lists of Equations
\newcommand{\listnameEquations}{List of Equations}
\newlistof{equations}{equ}{\listnameEquations}
\newcommand{\eqRef}[1]{%
\addcontentsline{equ}{equations}{\protect\numberline{\theequation}#1}\par}

masterFile.tex:

%! arara: lmkclean
%! arara: pdflatex: {   draft: yes }
%! arara: bibtex
%  arara: pdflatex: {   draft: yes }
%  arara: pdflatex: { synctex: yes }
%  arara: lmkclean
\documentclass[crop=false,float=true,class=scrartcl]{standalone}

\providecommand{\main}{.}
\input{\main/Subfiles/Preamble.tex}                     % Preamble [document configuration]

\begin{document}



\addcontentsline{toc}{section}{List of Contents}      \tableofcontents   \clearpage
\addtocontents  {toc}{\string\vspace{-1em}}
\addcontentsline{toc}{section}{List of Equations}     \listofequations   \clearpage
\addtocontents  {toc}{\string\vspace{-1em}}
\addcontentsline{toc}{section}{List of Code Listings} \lstlistoflistings \clearpage




\begin{equation} \mathrm{Equation\ null.1} \label{eq:null.1} \end{equation} \eqRef{Equation null.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ null.2} \label{eq:null.2} \end{equation} \eqRef{Equation null.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\addtocontents  {equ}{\string\vspace{+1em}}




\section{Section 1} \clearpage

\begin{equation} \mathrm{Equation\ 1.0.1} \label{eq:1.0.1} \end{equation} \eqRef{Equation 1.0.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 1.0.2} \label{eq:1.0.2} \end{equation} \eqRef{Equation 1.0.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 1.1} \clearpage

\begin{equation} \mathrm{Equation\ 1.1.1} \label{eq:1.1.1} \end{equation} \eqRef{Equation 1.1.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 1.1.2} \label{eq:1.1.2} \end{equation} \eqRef{Equation 1.1.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 1.2} \clearpage

\begin{equation} \mathrm{Equation\ 1.2.1} \label{eq:1.2.1} \end{equation} \eqRef{Equation 1.2.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 1.2.2} \label{eq:1.2.2} \end{equation} \eqRef{Equation 1.2.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\addtocontents  {equ}{\string\vspace{+1em}}




\section{Section 2} \clearpage

\begin{equation} \mathrm{Equation\ 2.0.1} \label{eq:2.0.1} \end{equation} \eqRef{Equation 2.0.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 2.0.2} \label{eq:2.0.2} \end{equation} \eqRef{Equation 2.0.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 2.1} \clearpage

\begin{equation} \mathrm{Equation\ 2.1.1} \label{eq:2.1.1} \end{equation} \eqRef{Equation 2.1.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 2.1.2} \label{eq:2.1.2} \end{equation} \eqRef{Equation 2.1.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 2.2} \clearpage

\begin{equation} \mathrm{Equation\ 2.2.1} \label{eq:2.2.1} \end{equation} \eqRef{Equation 2.2.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ 2.2.2} \label{eq:2.2.2} \end{equation} \eqRef{Equation 2.2.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\addtocontents  {equ}{\string\vspace{+1em}}




\textbf{\LARGE{Appendices}}
\addappheadtotoc
\begin{appendices}




\section{Section A} \clearpage

\begin{equation} \mathrm{Equation\ A.0.1} \label{eq:A.0.1} \end{equation} \eqRef{Equation A.0.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ A.0.2} \label{eq:A.0.2} \end{equation} \eqRef{Equation A.0.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection A.1} \clearpage

\begin{equation} \mathrm{Equation\ A.1.1} \label{eq:A.1.1} \end{equation} \eqRef{Equation A.1.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ A.1.2} \label{eq:A.1.2} \end{equation} \eqRef{Equation A.1.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection A.2} \clearpage

\begin{equation} \mathrm{Equation\ A.2.1} \label{eq:A.2.1} \end{equation} \eqRef{Equation A.2.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ A.2.2} \label{eq:A.2.2} \end{equation} \eqRef{Equation A.2.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\addtocontents  {equ}{\string\vspace{+1em}}




\section{Section B} \clearpage

\begin{equation} \mathrm{Equation\ B.0.1} \label{eq:B.0.1} \end{equation} \eqRef{Equation B.0.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ B.0.2} \label{eq:B.0.2} \end{equation} \eqRef{Equation B.0.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 1.1} \clearpage

\begin{equation} \mathrm{Equation\ B.1.1} \label{eq:B.1.1} \end{equation} \eqRef{Equation B.1.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ B.1.2} \label{eq:B.1.2} \end{equation} \eqRef{Equation B.1.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\subsection{Subsection 1.2} \clearpage

\begin{equation} \mathrm{Equation\ B.2.1} \label{eq:B.2.1} \end{equation} \eqRef{Equation B.2.1}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 

\begin{equation} \mathrm{Equation\ B.2.2} \label{eq:B.2.2} \end{equation} \eqRef{Equation B.2.2}
\lstinputlisting[caption=script.m]{\main/Scripts/script.m}
\clearpage 




\end{appendices}


\end{document}

Notes:

Use of \AtBeginDocument with listings counters comes from here.

  • 1
    Floats may end up outside the section they're defined in, of course, making section-specific numbering somewhat dubious. – Werner Aug 18 '16 at 16:42
  • 1
    You're asking a lot of questions and we should again glue something together? ;-) I am wondering about \addtocontents etc. for the ToC and other stuff. Isn't KOMA providing this out of the box? – user31729 Aug 18 '16 at 16:53
  • @Werner We're not worried. We hard-place [h] all of ours. tex's opinion be damned. – kando Aug 18 '16 at 16:59
  • @ChristianHupfer Shoot me the command that supercedes \addtocontents; it's certainly possible I missed something. This is unrelated to the question though. The tocs are only there to more easily line up the numbering for testing. – kando Aug 18 '16 at 17:00
  • 1
    The standalone * package * and the standalone * class* have quite different purposes and uses. That you need the first I can believe but not the second. – Ulrike Fischer Aug 18 '16 at 18:32
1

Alright, I'm not sure what worked for me before, or why, but this seems like it's fine to use now.

Place the following code in the preamble: This will provide the option to use \sectionedCounter{<ctr>} within the preamble or within the document to set the section depth when inserting the section number into float counters.

If using \sectionedCounter{\determineSection}, the lowest level section depth will be used.

Note: The last lines of code initiate the command. This is not needed but is included to note that when using listings, \AtBeginDocument{<cmd>} is needed when using the command inside of the preamble.

% Sectioned Counters:

% Save a copy of the original float counters.
\let\xtheequationOriginal\theequation
\let\xthefigureOriginal\thefigure
\let\xthetableOriginal\thetable
\let\xthelstlistingOriginal\thelstlisting

% Create blank commands which will later act as variable rich-format counters.
\newcommand{\sectionedEquation}{\arabic{equation}  }
\newcommand{\sectionedFigure  }{\arabic{figure}    }
\newcommand{\sectionedTable   }{\arabic{table}     }
\newcommand{\sectionedListing }{\arabic{lstlisting}}

% Set the float counters to the rich-format counter commands.
\let\theequation\sectionedEquation
\let\thefigure\sectionedFigure
\let\thetable\sectionedTable
\let\thelstlisting\sectionedListing


% Command: Determine Section
\newcommand{\determineSection}{%  [Provides full section counter of current section, independent of the section depth.]
  \ifnum\value{subsubsection} > 0
  \ifnum\value{paragraph}     > 0 
  \ifnum\value{subparagraph}  > 0     paragraph%
  \else                           subsubsection\fi
  \else                              subsection\fi
  \else                                 section\fi
}

% Command: Sectioned Counter
\newcommand{\sectionedCounterStyle}[2]{\csname the#2\endcsname--\arabic{#1}} 
  % Input #1: equation, lstlisting, table, figure
  % Input #2: \determineSection, \thesection, \thesubsection, ..
\newcommand{\sectionedCounter}     [1]{
  % Input #1: \determineSection,     section,     subsection, ..

  \counterwithout*{equation}  {section} 
  \counterwithout*{figure}    {section}
  \counterwithout*{table}     {section}
  \counterwithout*{lstlisting}{section}

  \counterwithout*{equation}  {subsection} 
  \counterwithout*{figure}    {subsection}
  \counterwithout*{table}     {subsection}
  \counterwithout*{lstlisting}{subsection}

  \counterwithout*{equation}  {subsubsection} 
  \counterwithout*{figure}    {subsubsection}
  \counterwithout*{table}     {subsubsection}
  \counterwithout*{lstlisting}{subsubsection}

  \counterwithout*{equation}  {paragraph} 
  \counterwithout*{figure}    {paragraph}
  \counterwithout*{table}     {paragraph}
  \counterwithout*{lstlisting}{paragraph}

  \counterwithout*{equation}  {subparagraph} 
  \counterwithout*{figure}    {subparagraph}
  \counterwithout*{table}     {subparagraph}
  \counterwithout*{lstlisting}{subparagraph}

  \counterwithin{equation}  {#1}   % Reset counter whenever there is a new \section
  \counterwithin{figure}    {#1}
  \counterwithin{table}     {#1}
  \counterwithin{lstlisting}{#1}   % The listings counter is not actually defined until \AtBeginDocument. 
                                   % Thus, if using this command (including listings) within the preamble, 
                                   %   use ``\AtBeginDocument{\sectionedCounter{<input>}}'' instead.

  \renewcommand{\sectionedEquation}[1]{\sectionedCounterStyle{equation}  {#1}}
  \renewcommand{\sectionedFigure  }[1]{\sectionedCounterStyle{figure}    {#1}}
  \renewcommand{\sectionedTable   }[1]{\sectionedCounterStyle{table}     {#1}}
  \renewcommand{\sectionedListing }[1]{\sectionedCounterStyle{lstlisting}{#1}}  
}

% Set float counters to include their full section number.
\AtBeginDocument{\sectionedCounter{\determineSection}}
% -\determineSection is chosen by default as a catch-all, such that
%   float counters adapt to any section level when running standalone subfiles
%   which do not contain a \sectionedCounter command.
%   It is assumed that in most cases, the author will set the counter depth to a specific level.

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.