# Bolding the first line of \item elements, question/answer sytle

I was wondering if it was possible to bold the first line of an enumerated list without effecting the remainder of the list. The output I want would be the same as:

\begin{enumerate}
\item \textbf{This is a question, what is the answer?} \\
This is the answer, in standard font!

\item \textbf{This is another question, what is the next answer?} \\
Here is the answer, also not in bold.
\end{enumerate}


Is it possible to do this without \textbf tags before every question, some way to automate it? using \begin{enumerate}\bfseries bolds everything.

There are some similar questions but the answers are either to just wrap each line in a tag or seem very inelegant.

• This is fairly easy to do, only if you're consistent with your input. For example, will you always end your first line with \\? Or, perhaps a blank line? – Werner Apr 7 '15 at 17:21
• what do you want to happen if the question requires more than one line? – barbara beeton Apr 7 '15 at 17:23
• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. In addition: There are packages for typesetting question/solutions etc, e.g. probsln, tasks, answers – user31729 Apr 7 '15 at 17:24
• I wouldn't use \\  to separate question and answer, rather than \item qqqq \\ aaaa I'd use custom commands such as \question{qqqqqq}\answer{aaaaa} then it is easy to specify whatever style you want – David Carlisle Apr 7 '15 at 17:24
• @Werner - this is for a fairly simple homework assignment, so I'm not to worried about that. Your answer was great. – afischer Apr 7 '15 at 17:48

Here are some options, depending on your input preference. They all produce the following output:

1. Consistent use of \\ to end the question:

\documentclass{article}

\newenvironment{QandA}
{\begin{enumerate}
\let\olditem\item
\long\def\item##1\\{\olditem{\bfseries ##1}\par}}
{\end{enumerate}}

\begin{document}

\begin{QandA}
\item This is a question, what is the answer? \\
This is the answer, in standard font!

\item This is another question, what is the next answer? \\
Here is the answer, also not in bold.
\end{QandA}

\end{document}


We define the QandA environment to be similar to an enumerate only with an updated definition of how \item works. The first step is to store the current version of \item in something like \olditem (via \let). Even though the traditional \item takes an optional argument and one should be careful when \letting macros with optional arguments, you're not using that here and it works fine under the circumstances.

Now, the redefinition of \item is made \long so it can support \paragraph break. Also, we require the parameter text of \item to end with \\ (that's what \def\item<stuff>\\{..} means). So, an explicit ending-\\ is required for this to work and successfully gobble what lies between \item and \\ (as #1). Once this pattern is matched, we set \olditem (whatever \item was at the beginning of the QandA environment), a limited scope bolding of the argument - {\bfseries #1} - and a forced \paragraph.

2. Consistent use of a blank/empty line to denote the difference between question and answer:

\documentclass{article}

\newenvironment{QandA}
{\begin{enumerate}
\let\olditem\item
\long\def\item##1\par{\olditem{\bfseries ##1}\par}}
{\end{enumerate}}

\begin{document}

\begin{QandA}
\item This is a question, what is the answer?

This is the answer, in standard font!

\item This is another question, what is the next answer?

Here is the answer, also not in bold.
\end{QandA}

\end{document}


The redefinition of \item is similar to that discussed in (1), except that a blank/empty line - converted to \par by TeX - is required.

3. Using readable markup to identify \questions and \answers:

\documentclass{article}

\newcommand{\question}[1]{\item{\bfseries #1}}
\newenvironment{QandA}
{\begin{enumerate}}
{\end{enumerate}}

\begin{document}

\begin{QandA}
\question{This is a question, what is the answer?}
\answer{This is the answer, in standard font!}

\question{This is another question, what is the next answer?}
\answer{Here is the answer, also not in bold.}
\end{QandA}

\end{document}

• Fantastic, thanks! I'll mark this as answered, but would you mind explaining how the command you created worked? – afischer Apr 7 '15 at 17:47
• @afischer: I've added some discussion on how the commands work. – Werner Apr 7 '15 at 17:54
• Is there a way to make this work with nested enumerate environments? When I attempt to nest, the pdf is never produced then I start getting input stack errors. – dayne Sep 6 '17 at 21:17
• @dayne: Does it matter which version/markup you use? – Werner Sep 6 '17 at 21:28
• I prefer the second -- I think the syntax is the easiest. – dayne Sep 6 '17 at 21:33

You can bold any number of words in the line. However, bolding a line is very difficult, since TeX must decide where to split the line, and this depends on the font. After the line is split, you need to return to the original tokens, but they are already "lost".

Many years ago Jim Sterken formulated this problem in 1983: http://www.tug.org/TUGboat/tb04-2/tb08sterken.pdf. Only in 1987 Anne Brüggemann-Klein suggested a solution: http://www.tug.org/TUGboat/tb08-2/tb18bruggemann.pdf.

Of course if you split the first line manually, this is trivial :)

\documentclass{article}
\pagestyle{empty}
\def\Item#1\\{\item\textbf{#1}\\}
\begin{document}
\begin{enumerate}
\Item This is a question\\ And this is an answer
\end{enumerate}
\end{document}