Text FormattingColoring Paragraph fails

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Hans Nieuwenhuis
Posts: 25
Joined: Tue Sep 16, 2008 6:05 am

Coloring Paragraph fails

Post by Hans Nieuwenhuis »

Hi,

I have a 'questions and answers' section in my document. I want the answers to be formatted in a special way: italics and blue text.

Example document:

Code: Select all

\documentclass[a4paper,pdftex]{report}
\usepackage{xcolor}

\newcommand{\answer}[1]{\textcolor{blue}{\textit{#1}}}

\begin{document}

\answer{
First sentence of answer.

Second sentence of answer.

\begin{enumerate}
  \item Item 1

  \item Item 2
\end{enumerate}
}

\end{document}
If I use simple text (no begin{enumerate}, no empty lines), this solution works.

But if I try to typeset this document using pdfLatex, I get the following error message:

Code: Select all

Runaway argument?
{\textit { First sentence of answer. 
! Paragraph ended before \@textcolor was complete.
<to be read again> 
                   \par 
l.20 }
I can solve this by placing a '\\' on the empty line between First sentence of answer and Second sentence of answer.

I also place a '\\' between Second sentence of answer and \begin{enumerate}.

However, then I get the following error message:

Code: Select all

Runaway argument?
{\textit { First sentence of answer. \\ Second sentence of answer. \\\ETC.
! Paragraph ended before \@textcolor was complete.
<to be read again> 
                   \par 
l.20 }
If I remove the

Code: Select all

\begin{enumerate}
  \item Item 1

  \item Item 2
\end{enumerate}
part, everything works fine.

How can I color all text which might be part of the argument to the \answer command?
Last edited by Hans Nieuwenhuis on Thu Nov 03, 2011 6:10 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Coloring Paragraph fails

Post by localghost »

This can't work. The logical approach would be to create an appropriate environment for blue and italicized text. For a final solution it could be helpful to see what you are after. Can you provide a visual example (sketch, scan, excerpt from other document)? At the moment all I see is something like this that would appear in the output.
First sentence of answer.
Second sentence of answer.

  1. Item 1
  2. Item 2
If you are going to write exercises and their respective solutions, there would be some packages or classes which support this.


Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
Hans Nieuwenhuis
Posts: 25
Joined: Tue Sep 16, 2008 6:05 am

Re: Coloring Paragraph fails

Post by Hans Nieuwenhuis »

I want exactly to produce what you are showing (the text in blue color and italic font).

I'm not making a 'questions and answers' section for a test/exam or so. There are no multiple answers a user has to choose from.

I will try to do something with an environment and let you know the results.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Coloring Paragraph fails

Post by localghost »

So this is the easiest method I can imagine.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}

\newenvironment{answer}{%
   \setlength{\parindent}{0pt}
   \itshape
   \color{blue}
}{}

\begin{document}
  \begin{answer}
    First sentence of answer.

    Second sentence of answer.
    \begin{enumerate}
      \item Item 1
      \item Item 2
    \end{enumerate}
  \end{answer}
\end{document}
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
Hans Nieuwenhuis
Posts: 25
Joined: Tue Sep 16, 2008 6:05 am

Re: Coloring Paragraph fails

Post by Hans Nieuwenhuis »

Yes, that works.

Thanks!
Post Reply