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.

kando
  • 1,338
  • 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? –  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
  • @kando: I can give you some clue if it is not KOMA, but the KOMA mafia will soon give some answers, I think ;-) –  Aug 18 '16 at 17:01
  • @kando: [h] doesn't hard-place a float. Beside this your question is much to long to my liking. Are you sure you want to use standalone as master class? – Ulrike Fischer Aug 18 '16 at 17:27
  • @UlrikeFischer Yep. No harm, no foul. (It is used in all of our master and subfiles, that any file may be run standalone. The subfiles were removed for the purposes of the MWE. I do not believe the standalone class is related to the issue.) – kando Aug 18 '16 at 17:30
  • 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
  • @UlrikeFischer Correct that it is not necessary without the subfiles. Removed from the preamble, as well as other clutter. – kando Aug 18 '16 at 18:46
  • @ChristianHupfer (Hmmm, no word from Martin Scharrer on KOMA.) As it turns out, you gave me the original answer. I don't know what I changed that things stopped working, other than that the \renewcommands are now within another command. – kando Aug 18 '16 at 19:17
  • @ChristianHupfer My answer is wrong. It gets the depth correct, but \renewcommand continues to have no effect on the syntax of the counter. This is a problem since larger section depths begin to mask that the final digit represents a float counter. What am I missing that \renewcommand cannot affect the counter syntax? (I thought that \counterwithin without an * was needed to allow \renewcommand to be able to change the syntax. Really, \counterwithin is changing the counter and \renewcommand continues to run without an error, but continues to whiff as well.) – kando Aug 19 '16 at 12:46
  • @kando: Looking at your question/answer code I don't understand really what you're after. But I've got the impression that most is ill-designed and should be revised. LaTeX is quite nice for doing standard setups, that's why there are the standard classes, doing more requires knowledge which packages provide which features and some knowledge of TeX/LaTeX internals. If you build a house you don't start with the roof top-down, do you? ;-) –  Aug 19 '16 at 12:50
  • @ChristianHupfer : I'm pretty set toward getting this to work because of our document structure at work. (The alternative is that we just use MS Word, where someone has gotten it to work already. Which is gross.) – kando Aug 19 '16 at 13:06
  • @ChristianHupfer We want a segment of code which creates a one-line command which can be used to set the section depth, such that the section number portion of the counter displays to that depth and the float number portion of the counter resets at that depth. The resetting is easy, with \counterwithin, but \counterwithin separates the section number from the float number with the same punctuation (a period). This is terrible, so we want the added ability to change the counter from <section>.<float>, which appears as <2.1.3.5> to <section>-<float>, which appears as <2.1.3-5>. – kando Aug 19 '16 at 13:09
  • @kando: Well, I don't get it. Your question is too big and I don't attack this question since it is too tedious to pursue all the 'weird' settings which seem to be glued together from a bunch of different codes –  Aug 19 '16 at 16:51
  • @ChristianHupfer Just figured it out. Will update when code is smoothed to fix increase in counter size within ToC lists of floats. Problem was: Accidentally included [n.inputs] in \renewcommand{label}[n.inputs]{def}. Remove [n.inputs], no problems. Copypasta error I think. – kando Aug 19 '16 at 16:55
  • @ChristianHupfer Also, I had to remove the creation of an arbitrary command and then setting that \the[float] to that command (Example: \let\theequation\sectionedEquation). I don't know what was going wrong there, but I don't have the time to delve on that one : j – kando Aug 19 '16 at 17:01

1 Answers1

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.
Werner
  • 603,163
kando
  • 1,338