1

I've been reading LaTeX documentation, but I can't seem to figure out how I would create a new command that is basically a section with different text (ie. if I call \testsection it would make a Test Section n, where n is the test section number.

How would I also specify which digit to use (ie 1/2/3 vs 1.1/1.2/1.3 vs 1.1.1/1.1.2/1.1.3, etc)?

I found a workaround of the below, but that changes \subsection, which I would like to avoid. It also doesn't autonumber for some reason (it's stuck at 1).

\renewcommand{\thesubsection}{Problem \arabic{section}}
\newcommand{\problem}{\subsection{}}
0

2 Answers 2

2

In LaTeX you define counters using the \newcounter command. For example,

\newcounter{test}

defines a counter called test. If you want this counter to count "inside" another counter, so that it is automatically reset each time that counter changes, you add the name of the "parent" counter as an optional argument. For example, to make test counter "inside" the section counter you would write:

\newcounter{test}[section]

The value of the test counter is printed using \thetest. We can change how this is formatted using:

\renewcommand\thetest{\thesection.\alph{test}}

The code below wraps all of this into a new section like macro called \testing, which produces:

enter image description here

Here is the full code:

\documentclass{article}
\newcounter{test}[section]
\renewcommand\thetest{\thesection.\alph{test}}
\newcommand\testing[1]{%
   \refstepcounter{test}%
   \textbf{\thetest~#1}
}
\begin{document}

    \section{First section}

    \testing{one test}
    \testing{two test}
    \testing{three test}

    \section{First section}

    \testing{one test}
    \testing{two test}
    \testing{three test}

\end{document}
2

Perhaps a theorem-like environment testsection whose testsection-counter is reset whenever the counter of a sectioning-command is stepped does the trick for you?

\documentclass{article}
\usepackage{hyperref}

\newtheorem{testsection}{Test Section}%

\makeatletter
%--------------------------------------
\@addtoreset{testsection}{section}
\@addtoreset{testsection}{subsection}
\@addtoreset{testsection}{subsubsection}
\@addtoreset{testsection}{paragraph}
\@addtoreset{testsection}{subparagraph}
%--------------------------------------
\newcommand\CntValNotZero[4]{%
  \ifnum\value{#3}<1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {#4}{\csname the#1#3\endcsname#2}%
}%
\renewcommand\thetestsection{%
  \CntValNotZero{}{.}{subparagraph}{%
    \CntValNotZero{}{.}{paragraph}{%
      \CntValNotZero{}{.}{subsubsection}{%
        \CntValNotZero{}{.}{subsection}{%
          \CntValNotZero{}{.}{section}{}%
        }%
      }%
    }%
  }%
  \arabic{testsection}%
}%
\@ifpackageloaded{hyperref}{%
  \renewcommand\theHtestsection{%
    \CntValNotZero{H}{.}{subparagraph}{%
      \CntValNotZero{H}{.}{paragraph}{%
        \CntValNotZero{H}{.}{subsubsection}{%
          \CntValNotZero{H}{.}{subsection}{%
            \CntValNotZero{H}{.}{section}{}%
          }%
        }%
      }%
    }%
    \arabic{testsection}%
  }%
}{}%
\makeatother

\pagestyle{empty}
\enlargethispage{1in}

\begin{document}
\vspace*{-1in}%

\section{First Section}

\begin{testsection}\label{testsectionlabel1.1}%
A test section inside First Section.
\end{testsection}

\begin{testsection}\label{testsectionlabel1.2}%
Another test section inside First Section.
\end{testsection}

\subsection{First Section's First Subsection}

\begin{testsection}\label{testsectionlabel1.1.1}%
A test section inside First Section's First Subsection.
\end{testsection}

\begin{testsection}\label{testsectionlabel1.1.2}%
Another test section inside First Section's First Subsection
\end{testsection}

\subsection{First Section's Second Subsection}

\begin{testsection}\label{testsectionlabel1.2.1}%
A test section inside First Section's Second Subsection.
\end{testsection}

\begin{testsection}\label{testsectionlabel1.2.2}%
Another test section inside First Section's Second Subsection
\end{testsection}

\section{Second Section}

\begin{testsection}\label{testsectionlabel2.1}%
A test section inside Second Section.
\end{testsection}

\begin{testsection}\label{testsectionlabel2.2}%
Another test section inside Second Section.
\end{testsection}

\subsection{Second Section's First Subsection}

\begin{testsection}\label{testsectionlabel2.1.1}%
A test section inside Second Section's First Subsection.
\end{testsection}

\begin{testsection}\label{testsectionlabel2.1.2}%
Another test section inside Second Section's First Subsection
\end{testsection}

\subsection{Second Section's Second Subsection}

\begin{testsection}\label{testsectionlabel2.2.1}%
A test section inside Second Section's Second Subsection.
\end{testsection}

\begin{testsection}\label{testsectionlabel2.2.2}%
Another test section inside Second Section's Second Subsection
\end{testsection}

Referencing:

\verb|\ref{testsectionlabel1.1}|:  \ref{testsectionlabel1.1}

\verb|\ref{testsectionlabel1.2}|:  \ref{testsectionlabel1.2}

\verb|\ref{testsectionlabel1.1.1}|:  \ref{testsectionlabel1.1.1}

\verb|\ref{testsectionlabel1.1.2}|:  \ref{testsectionlabel1.1.2}

\verb|\ref{testsectionlabel1.2.1}|:  \ref{testsectionlabel1.2.1}

\verb|\ref{testsectionlabel1.2.2}|:  \ref{testsectionlabel1.2.2}

\verb|\ref{testsectionlabel2.1}|:  \ref{testsectionlabel2.1}

\verb|\ref{testsectionlabel2.2}|:  \ref{testsectionlabel2.2}

\verb|\ref{testsectionlabel2.1.1}|:  \ref{testsectionlabel2.1.1}

\verb|\ref{testsectionlabel2.1.2}|:  \ref{testsectionlabel2.1.2}

\verb|\ref{testsectionlabel2.2.1}|:  \ref{testsectionlabel2.2.1}

\verb|\ref{testsectionlabel2.2.2}|:  \ref{testsectionlabel2.2.2}

\end{document}

enter image description here

You must log in to answer this question.