3

I defined an environment that looks like this:

\newcounter{question}
\newenvironment{question}[2][]{\refstepcounter{question}\noindent \textbf{Question~\thequestion: #1} #2\\}

In my latex document I use it like this:

\question{What is the difference between System.gc() and Runtime.gc()?}
\question{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

And this generates the following output:

enter image description here

However, I would like that the word 'held' of the second line in question 7 has the same indent as the word if. How can I achieve that?

7
  • 1
    Why aren't you using a list environment (enumitem would make it much easier). But if you do not want, then consider putting your "Question #:" into a fixed-width box and using \hangindent.
    – TeXnician
    Apr 9, 2017 at 8:05
  • @ChristianHupfer Thanks for the correction. My browser spellchecker isn't that smart (identifying TeX commands).
    – TeXnician
    Apr 9, 2017 at 8:09
  • 1
    The use of \question is wrong this way! I think the 2nd argument is a misuse as well.
    – user31729
    Apr 9, 2017 at 8:22
  • The reason that I'm not using a list environment is that these kind of questions are distributed over a big document and in between there is many other content.
    – user6189
    Apr 9, 2017 at 8:29
  • 1
    What's the problem with this? You can use a list environment and let it use a specific counter as a label. There's no problem with that.
    – TeXnician
    Apr 9, 2017 at 8:46

3 Answers 3

3

One way is to use a list environment with one \item for each question. And not to reset the counter.

\documentclass{article}
\usepackage{lipsum}
\usepackage{calc}
\newcounter{question}
\newcommand\MyQuestionList[1]{%
  \begin{list}{%
      \refstepcounter{question}%
      \textbf{Question \thequestion:}%
    }{%
      \settowidth{\labelwidth}{\textbf{Question \thequestion:}}
      \setlength{\leftmargin}{\labelwidth+\labelsep}%
    }
  \item #1
  \end{list}%
}

\begin{document}
\lipsum[1]

\MyQuestionList{\lipsum[2]}

\MyQuestionList{New qestion}

Set the question couter to 25, i.e. the next question is 26.
\setcounter{question}{25}

\MyQuestionList{\lipsum[3]}

\lipsum[4]
\end{document}

I have set the counter to 25 just to show that the indentation follows the label for each question.

enter image description here

0
3

It's easier to define a new enumerate-like environment with enumitem. For a continuous numbering, use the resume option of enumitem:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{enumitem}

\newlist{questions}{enumerate}{1}
\setlist[questions]{label=\bfseries Question \arabic*: , align=left, widest=\textbf{999}, leftmargin=*, ref=\arabic*}
\def\qu{\item}

\begin{document}

\begin{questions}%
    \qu What is the difference between System.gc() and Runtime.gc()?\label{q-1}
    \qu If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?
    \setcounter{questionsi}{99}
    \qu Some test text. Some test text. Some test text. Some test text. Some test text. 
\end{questions}
Proof of \ref{q-1}. —
\begin{questions}[resume]
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
\end{questions}

\end{document} 

enter image description here

You also can have a variable list left margin, according the value of the counter (between 1 and 9, or between 10 and 99, &c.):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{enumitem}

\newlist{questions}{enumerate}{1}
\setlist[questions]{label=\bfseries Question \arabic*: , align=left, leftmargin=*, ref=\arabic*}
\def\qu{\item}

\begin{document}

\begin{questions}%
    \qu What is the difference between System.gc() and Runtime.gc()?\label{q-1}
    \qu If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?
    \setcounter{questionsi}{97}
\end{questions}
Proof of \ref{q-1}. —
\begin{questions}[resume, widest=\textbf{99}, ]%
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
    \qu Another test text. Another test text. Another test text. Another test text. Another test text.
\end{questions}
\begin{questions}[resume, widest=\textbf{999}, ]
    \qu Some test text. Some test text. Some test text. Some test text. Some test text.
    \qu Another test text. Another test text. Another test text. Another test text. Another test text.
\end{questions}

\end{document} 

enter image description here

9
  • That does not comply with what the OP wants. He wants it to be numbered continously.
    – TeXnician
    Apr 9, 2017 at 9:31
  • @TeXnician: What do you mean exactly with ‘numbered continuously’? There's no gap in enumerate environments numberings.
    – Bernard
    Apr 9, 2017 at 9:59
  • See StefanH's answer for explanation (numbering continously in document, not per list environment).
    – TeXnician
    Apr 9, 2017 at 10:10
  • But that is easy to do with enumitem and the resume option. I'll post an example.
    – Bernard
    Apr 9, 2017 at 10:12
  • Yes, but do not expect an OP to read manuals ;) I'm glad you edited (I think this short answer is more convenient than the whole definition of an own list).
    – TeXnician
    Apr 9, 2017 at 10:14
1

Just another solution without a list (actually two solutions, one with \hangindent and one without):

\documentclass[a4paper]{article}

\newcounter{question}
\newcommand{\qsparbox}[2][\relax]{%
    \refstepcounter{question}%
    \ifx#1\relax%
        \setbox0\hbox{\textbf{Question~\thequestion:}}%
    \else%
        \setbox0\hbox{\textbf{Question~\thequestion: #1}}%
    \fi%
    \noindent\usebox0\hskip1em\parbox[t]{\dimexpr\textwidth-1em-\wd0}{#2}\\%
}
\newcommand{\qshangindent}[2][\relax]{%
    \refstepcounter{question}%
    \ifx#1\relax%
        \setbox0\hbox{\textbf{Question~\thequestion:}}%
    \else%
        \setbox0\hbox{\textbf{Question~\thequestion: #1}}%
    \fi%
    \par\hangindent3em%
    \noindent\usebox0\hskip1em#2\par%
}


\begin{document}
Using \verb|\parbox|:

\qsparbox{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\qsparbox[test]{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\vspace{1em}
Using \verb|\hangindent|:
\qshangindent{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}

\qshangindent[test]{If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object?}
\end{document}

results

You must log in to answer this question.

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