2

I want to make a page with series of questions and answers. I want to enumerate questions with prefix Q and answers with prefix A. Moreover, I would like to have groups of Q&A that can be identified by group number. Let me show you with an example:

Q1 How many countries are there in the world?
A1 There are 196 countries in the world today. Unless you don't count Taiwan…
Q1.1 How many countries are there in Europe?
A1.1 Approximately 50 countries.
Q1.2 How many countries are there in Asia?
A1.2 48.
Q2 What is the population of the world?
A2 Around 7.4 billion people
Q2.1 What is the population of the US?
A2.1 324,532,828 as of Sunday, September 4, 2016.
Q2.1.1 What is the population of Nevada state?
A2.1.1 Around 2,943,409.

Edit: I tried this code:

\documentclass{article}
\usepackage{enumerate}
\usepackage[shortlabels]{enumitem}

\begin{document}
\begin{enumerate}[start=1,label={(\bfseries Q\arabic*):}]
\item How many countries are there in the world?

There are 196 countries in the world today. Unless you don't count Taiwan…
\begin{enumerate}
\item How many countries are there in Europe?

Approximately 50 countries.

\item How many countries are there in Asia?

48.
\end{enumerate}
\item What is the population of the world?

Around 7.4 billion people
\begin{enumerate}
\item What is the population of the US?

324,532,828 as of Sunday, September 4, 2016.
\begin{enumerate}
\item What is the population of Nevada state?
\end{enumerate}
Around 2,943,409.
\end{enumerate}
\end{enumerate}
\end{document} 

enter image description here

5
  • I think I have to somehow add more idea to this answer: tex.stackexchange.com/a/229542/93219 however I need to use two prefix (Q and A) at the same time!
    – Sadegh
    Sep 5, 2016 at 17:57
  • 2
    "I want ... I want ..." Your posting, as it stands, comes across as a pure do-this-for-me demand, especially as it shows between little and no research effort. For all we know, you're not even looking for a LaTeX- or TeX-based solution. Please edit your posting to (a) show some research effort and (b) tell us what -- preferably in the form of compilable LaTeX or Plain-TeX code -- you've tried so far.
    – Mico
    Sep 5, 2016 at 17:59
  • 3
    I think this is possible with the usual answer/problem/exam - packages, such as probsoln, answers, exsheet etc.
    – user31729
    Sep 5, 2016 at 18:00
  • @Mico you are wrong, I tried some solutions, but non of them was working. Maybe you didn't read the comment before yours. Also "I want" is just a way to start the quesiton and ask, next time I write "ones want"... Anyways I update the question.
    – Sadegh
    Sep 5, 2016 at 18:17
  • @Chrtian Hupfer tnx! I am reading manuals of them.
    – Sadegh
    Sep 5, 2016 at 18:18

2 Answers 2

2

Given that there are packages available to typeset examples, problems and answers etc., it is not clear there is much point in doing this yourself. Nonetheless, if you wish to roll-your-own, I would try something like the following.

I drop enumerate. It makes no sense to load it with enumitem, especially if you are asking the latter to emulate the former with shortlabels.

Based on your question I assume you want:

  • no indentation from the left margin;
  • no additional vertical spacing between items in the list, regardless of level.

Since this is not entirely straightforward to do, if you didn't mean it, you should have been clearer about your desiderata.

Based on my best attempt to understand your desiderata from your question and code, I used a 3-level list questions defined with enumitem. Within the list \qn creates a question and \ans creates an answer.

Hence,

\begin{questions}
  \qn How many countries are there in the world?
  \ans There are 196 countries in the world today. Unless you don't count Taiwan…
  \begin{questions}
    \qn How many countries are there in Europe?
    \ans Approximately 50 countries.
    \qn How many countries are there in Asia?
    \ans 48.
  \end{questions}
  \qn What is the population of the world?
  \ans Around 7.4 billion people
  \begin{questions}
    \qn What is the population of the US?
    \ans 324,532,828 as of Sunday, September 4, 2016.
    \begin{questions}
      \qn What is the population of Nevada state?
      \ans Around 2,943,409.
    \end{questions}
  \end{questions}
\end{questions}

produces

questions

Code:

\documentclass{article}
\usepackage{enumitem}
\makeatletter
\newlist{questions}{enumerate}{3}
\setlist[questions]{itemindent=0pt, labelwidth=!,align=parleft,itemsep=\lineskip,parsep=\lineskip}
\setlist[questions,1]{label=Q\,\arabic*,leftmargin=1.5\parindent,labelindent=0pt,ref=\arabic*}
\setlist[questions,2]{label=Q\,\arabic{questionsi}.\arabic*,leftmargin=.5\parindent,labelindent=-1.5\parindent,ref=\arabic{questionsi}.\arabic*,topsep=\lineskip,partopsep=\lineskip}
\setlist[questions,3]{label=Q\,\arabic{questionsi}.\arabic{questionsii}.\arabic*,leftmargin=.5\parindent,labelindent=-2\parindent,ref=\arabic{questionsi}.\arabic{questionsii}.\arabic*,topsep=\lineskip,partopsep=\lineskip}
\newcommand*\qn{\stepcounter{cntquestions}\item\label{qn:\thecntquestions}}
\newcommand*\ans{\item[A\,\ref{qn:\thecntquestions}]}
\newcounter{cntquestions}
\setcounter{cntquestions}{0}
\makeatother
\begin{document}
\begin{questions}
  \qn How many countries are there in the world?
  \ans There are 196 countries in the world today. Unless you don't count Taiwan…
  \begin{questions}
    \qn How many countries are there in Europe?
    \ans Approximately 50 countries.
    \qn How many countries are there in Asia?
    \ans 48.
  \end{questions}
  \qn What is the population of the world?
  \ans Around 7.4 billion people
  \begin{questions}
    \qn What is the population of the US?
    \ans 324,532,828 as of Sunday, September 4, 2016.
    \begin{questions}
      \qn What is the population of Nevada state?
      \ans Around 2,943,409.
    \end{questions}
  \end{questions}
\end{questions}
\end{document}
4
  • This is exactly what I want! tnx
    – Sadegh
    Sep 6, 2016 at 9:23
  • btw, I use this code in my thesis. all the answers are clickable and jump to relevant question. do you know how can I remove this link?
    – Sadegh
    Sep 6, 2016 at 9:25
  • I think I need to modify \newcommand*\ans{\item[A\,\ref{qn:\thecntquestions}]} and force it not become a link, but I dont know how
    – Sadegh
    Sep 6, 2016 at 9:32
  • @Woeitg Presumably, you are loading hyperref.
    – cfr
    Sep 6, 2016 at 12:26
3

I am sure one of packages mentionion by Christian Hupfer would do a much better job, but here is a quick solution to your specific problem.

As mentioned by cfr, one shouldn't load two packages which tackles the same problems, like enumerate and enumitem. enumerate has therefore been disabled.

Output

enter image description here

Code

\documentclass{article}
\usepackage[shortlabels]{enumitem}

\newcommand{\Answer}[1]{%
\item[\bfseries(A\arabic{enumi}):] #1
}
\newcommand{\SubAnswer}[1]{%
\item[(a\arabic{enumii}):] #1
}

\begin{document}
  \begin{enumerate}[start=1,label={\bfseries(Q\arabic*):}]
    \item How many countries are there in the world?

    \Answer{Depends on who is doing the counting}
    \begin{enumerate}
      \item How many countries are there in Europe?

      Approximately 50 countries.

      \item How many countries are there in Asia?

      48.
    \end{enumerate}
    \item What is the population of the world?

    \Answer{Around 7.4 billion people}
    \begin{enumerate}
      \item What is the population of the US?

      324,532,828 as of Sunday, September 4, 2016.
      \begin{enumerate}
        \item What is the population of Nevada state?
      \end{enumerate}
      \SubAnswer{Around 2,943,409.}
    \end{enumerate}
  \end{enumerate}
\end{document}
[![enter image description here][1]][1]
3
  • 1
    thanks for answer, I give negative vote by mistake, I cannot fix it until you modify something!! cheers
    – Sadegh
    Sep 5, 2016 at 19:10
  • @Woeitg no problem.
    – Runar
    Sep 5, 2016 at 19:12
  • 1
    Don't load enumerate and enumitem, especially if using the latter with shortlabels. It makes no sense.
    – cfr
    Sep 6, 2016 at 0:49

You must log in to answer this question.

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