Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
+1 Great, this makes it a lot easier to handle. –  petrichor Nov 24 '11 at 13:54
1  
I borrowed the titleformat from this post by the way. (Thanks, @Werner.) –  Torbjørn T. Nov 24 '11 at 13:57
    
Thanks for the reference, I thanked there with +1 :) –  petrichor Nov 24 '11 at 14:04

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.

share|improve this answer
1  
+1 This was the exact answer I was wondering. –  petrichor Nov 24 '11 at 13:55
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.