24

I wish to add some text prior to the number given to a subsection, e.g.:

Task 1.1  Blablabla
...
Task 1.2  Blablabla

How?

2
  • 1
    Can you please be a little bit more specific? Do you need this Task text for a special environment, like a theorem or for what? BTW: Welcome to TeX.sx!
    – Mensch
    Commented Nov 10, 2012 at 22:23
  • Thanks. No special environment. Task is just an example text. I would like to have a word like that before the number for a subsection, and that's all.
    – Steeven
    Commented Nov 10, 2012 at 22:33

2 Answers 2

27

To add text before the section, you can use:

\renewcommand{\thesection}{Text \arabic{section}}

enter image description here

or for subsection you can use:

\renewcommand{\thesubsection}{Text \arabic{section}}

enter image description here

Code: section

\documentclass{article}

\renewcommand{\thesection}{Text \arabic{section}}

\begin{document}
\section{First section}
Some text.

\section{Second section}
Some more text.
\end{document}

Code: subsection:

\documentclass{article}

\renewcommand{\thesubsection}{Text \arabic{subsection}}

\begin{document}
\section{First section}
\subsection{First sub section}
Some text.
\subsection{Second sub section}
Some more text.
\end{document}
6
  • This is great and works. But some other problem appears when I use the \renewcommand{\thesubsection}{Text \arabic{section}}. The numbering of the subsection suddenly doesn't work. @Peter, if you make two subsections in your example, they both have the number 1. Can that be fixed?
    – Steeven
    Commented Nov 10, 2012 at 22:42
  • 1
    Clearly LaTeX is not smart enough to know that when I said \arabic{section}, that I obviously meant \arabic{subsection}. :-) Anyway, sorry about that mistake - have updated answer. Commented Nov 10, 2012 at 22:47
  • Precisely ;) Thanks alot. (By the way, you must update it in two places in your answer...)
    – Steeven
    Commented Nov 10, 2012 at 23:50
  • @Steeven: Two places? Only the \renewcommand needed updating. I added two subsections to show that it functioned as desired. Commented Nov 10, 2012 at 23:56
  • 2
    Oh well, I just meant the single line \renewcommand{\thesubsection}{Text \arabic{section}} above your example.
    – Steeven
    Commented Nov 11, 2012 at 13:37
30

Peter's answer has the disadvantage of printing the text you put before subsection numbers in references to the subsections and in the ToC, as well.

To avoid that, instead of renewing the meaning of \thesubsection, it's better to load the titlesec package and define

\titleformat{\subsection}{\normalfont\large\bfseries}{Task \thesubsection}{1em}{}

MWE:

\documentclass{article}

\usepackage{titlesec}
\titleformat{\subsection}{\normalfont\large\bfseries}{Task \thesubsection}{1em}{}

\begin{document}
\tableofcontents
\section{A section}
\subsection{First subsection}\label{subsec:first}
Some text from subsection \ref{subsec:second}.
\subsection{Second subsection}\label{subsec:second}
Some text from subsection \ref{subsec:first}.
\end{document} 

Output:

enter image description here

If you want similar behavior for sections, you can use something like

\titleformat{\section}{\normalfont\Large\bfseries}{Text \thesection}{1em}{}

If you also want that word before subsection numbers in the ToC, add the following code to your preamble

\usepackage[titles]{tocloft}
\renewcommand{\cftsubsecpresnum}{Task\space}
\newlength\mylength
\settowidth\mylength{\cftsubsecpresnum}
\addtolength\cftsubsecnumwidth{\mylength}

to get

enter image description here

1
  • +1, this solution can also be used to add text to the sections without affecting the subsection. Commented Sep 21, 2016 at 18:10

You must log in to answer this question.

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