7

I prepare a document where there are questions as follows and the relevant content

\section*{Question 1}
...
\section*{Question 2}
...
\section*{Question 3}
...
...

I want a counter instead of writing 1,2,3 such as

\section*{Question \qno}
...
\section*{Question \qno}
...
\section*{Question \qno}
...
...

where \qno will be automatically incremented and inserted as a number in the document. I read this post, but it's not something I need since I don't need cross-referencing.

Is there a way to initialize a counter and use it in LaTeX or is there an alternative solution for me to generate sections with name Question # in my document?

petrichor
  • 1,869

2 Answers2

8

One way is to use titlesec to customize how \section looks.

\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}[runin]{\large\bfseries}{}{0pt}{Question \thesection.}
\newcommand{\question}{\section{}}
\begin{document}
\question
\question
\end{document}

enter image description here

Torbjørn T.
  • 206,688
7

A direct solution to your problem is the following.

\documentclass{article}
\begin{document}

\newcounter{quest}
\setcounter{quest}{1}
\newcommand{\qno}{\arabic{quest}\stepcounter{quest}}

\section*{Question \qno}
\section*{Question \qno}

\end{document}

However, this seems like an abuse of the section command. You should instead change the presentation of sections, or define a new environment.

EDIT:

See Torbjørn's answer for how to customise sections.

qubyte
  • 17,299
  • Thanks for the +1. I really do recommend Torbjørn's answer though. It's far cleaner than mine. If you do what I put above, you're essentially declaring a counter when the one that section uses is feeling lonely and forgotten. – qubyte Nov 24 '11 at 13:59
  • I agree. T was unpleasant with using section out of each question part. Declaring a new question makes it easier to write and read. – petrichor Nov 24 '11 at 14:06
  • Are you using sections in your document as well though, or is this for something like a problem set for some students? – qubyte Nov 24 '11 at 14:08
  • Yes, it a very simple small document, it is a problem set for the students. I'm not using sections. – petrichor Nov 24 '11 at 14:11