0

I have not found a way to remove everything after the initial number (in my case a letter) from subsection numbering of a particular section in an article document. I have

Section
A.1 subsection
B.2 subsection
C.3 subsection

and I want

Section
A subsection
B subsection
C subsection

I thought that I could dig the answer out of this question but I couldn't. I didn't find any related questions, only questions that change subsections of the whole document. I managed to put the letters in instead of numbers with this code:

\newcounter{alphasect}
\def\alphainsection{0}

\let\oldsection=\subsection
\def\subsection{%
    \ifnum\alphainsection=1%
    \addtocounter{alphasect}{1}
    \fi%
    \oldsection}%

\renewcommand\thesection{%
    \ifnum\alphainsection=1% 
    \Alph{alphasect}
    \else%
    \arabic{section}
    \fi%
}%

\newenvironment{alphasection}{%
    \ifnum\alphainsection=1%
    \errhelp={Let other blocks end at the beginning of the next block.}
    \errmessage{Nested Alpha section not allowed}
    \fi%
    \setcounter{alphasect}{0}
    \def\alphainsection{1}
}{%
    \setcounter{alphasect}{0}
    \def\alphainsection{0}
}%

I use it as follows

\begin{alphasection}
%all the subsections
\end{alphasection}

I barely understand LaTeX, this is my second document. I got the code above from this site and intuitively I would say, that the desired result is obtainable by modifying the same script. I would appreciate a solution very much so I could go on with writing my thesis instead of wondering LaTeX-programming.

EDIT: the question is about changing one section, not all the sections of the document. So \renewcommand\thesubsection{\Alph{subsection}} does not help. Zarko's other suggestion is a good hack, but unfortunately leaves the subsections out from the table of contents. They should be there.

5
  • Welcome to TeX.SE!
    – Mensch
    Nov 18, 2021 at 14:33
  • Does \documentclass{article} \renewcommand\thesubsection{\Alph{subsection}} \begin{document} \section{Introduction} \subsection{First subsection} \subsection{Second subsection} \subsection{Third subsection} \end{document} gives what you after?
    – Zarko
    Nov 18, 2021 at 14:44
  • I want letters only in one section's subsection. @Zarko answer changes all the subsections of the document.
    – jvkloc
    Nov 18, 2021 at 14:49
  • @Jonne, very strange requirement ;-.(. However, than see the following: \documentclass{article} \begin{document} \section{Introduction} \subsection*{A First subsection} \subsection{Second subsection} \subsection{Third subsection} \end{document} if gives what you after ...
    – Zarko
    Nov 18, 2021 at 14:54
  • It's otherwise ok, but now the subsections are not in the table of contents. They should be there, too.
    – jvkloc
    Nov 18, 2021 at 14:58

2 Answers 2

2

Well two things. First, the problem is here:

\renewcommand\thesection{%
    \ifnum\alphainsection=1% 
    \Alph{alphasect}
    \else%
    \arabic{section}
    \fi%
}%

You're changing the numbering of sections, not subsections. Changing it to:

\renewcommand\thesubsection{%
    \ifnum\alphainsection=1% 
    \Alph{alphasect}
    \else%
    \arabic{subsection}
    \fi%
}%

should give the desired results. But, I would be more inclined to scrap all of this in favor of:

\NewDocumentEnvironment{alphasection}{} % ❶
   {%
      \RenewExpandableDocumentCommand{\thesubsection}{} % ❷
         {\Alph{subsection}}%
   }  
   {}

Note that in general, \NewDocumentEnvironment ❶ (along with \NewDocumentCommand and their relatives) is preferable to \newcommand for defining new commands.

Environments execute within a group, meaning that any redefinitions ❷ will revert at the end of the environment. Also, here we define the subsection number using \RenewExandableDocumentCommand so that we'll get the correct value if there is a reference to the number or if it's printed in the table of contents.

1
  • Yes, this works. I didn't notice the last 'subsection' in the code while changing a few other 'sections' to 'subsection'. I stil would like to change the internal numbering of theorems and definitions etc. to match the letter. Now I have A subsection theorem 6.1 instead of A subsection theorem A.1
    – jvkloc
    Nov 18, 2021 at 15:18
2

I saw your "I have..." and "but I want..." but I did not follow the code in your MWE. I do not understand why you are asking for unnumbered sections but double numbering for subsections. The following MWE produces your "I have" and "I want". Perhaps it will be of some help.

% subsecnumprob.tex  SE 623022

\documentclass{article}

\begin{document}

\renewcommand{\thesection}{}
\renewcommand{\thesubsection}{\Alph{subsection}.\arabic{subsection}}

\section{First section}
\subsection{First subsection}
\subsection{Second subsection}

\renewcommand{\thesubsection}{\Alph{subsection}}
\section{Second section}
\subsection{Third subsection}
\subsection{Fourth subsection}

\renewcommand{\thesubsection}{\Alph{subsection}.\arabic{subsection}}
\section{Third section}
\subsection{Fifth subsection}
\subsection{Sixth subsection}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .