2

So.... I'm trying to create (what I assume) is to be a simple command that tries to do some nice formatting. Here is what I have.

\usepackage{enumitem}
\newenvironment{question}{\bf}{\normalfont}
\newenvironment{answer}{\it}{\normalfont}
\newcommand{\qna}[2]{
    \fcolorbox{black}{yellow}{
        \begin{question}
            #1
        \end{question}
        \begin{answer}
            #2
        \end{answer}
    }
}

Now, this seems to work perfectly when I input any string.

\qna{Here's a q}{Here's an answer}

BUT I'm getting into issues when I try and pass in a non string as my second variable. Example

\qna{Here's a q}{Here an answer in list format
\begin{enumerate}
    \item answer 1
    \item answer 2
\end{enumerate}
}

The error I'm getting is

Something's wrong--perhaps a missing \item. }

I don't see what I'm doing wrong and what a potential way of fixing it would be.

Minimal Working Example:

\documentclass[11pt]{book}

\usepackage{enumitem}
\usepackage{xcolor}

\newenvironment{question}{\bf}{\normalfont}
\newenvironment{answer}{\it}{\normalfont}
\newcommand{\qna}[2]{
    \fcolorbox{black}{yellow}{
        \begin{question}
            #1
        \end{question}
        \begin{answer}
        #2
        \end{answer}
    }
}


\begin{document}
    \qna{test}{test}
\end{document}
  • 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}. – user31729 Jan 16 '15 at 9:44
  • 3
    (1) you should not be using \it and \bf, those are not LaTeX, (2) your problem might be that the colorbox need what ever it is surrounding to be a box, at least when you are adding lists. Try addin a minipage. BTW: Are you sure you want to use a syntax like this, it seems rather cumbersome. Have a look at the environ package, that might be useful. – daleif Jan 16 '15 at 9:44
  • 2
    please always post complete test documents not fragments so this is not tested but \fcolorbox like \mbox is a hmode (LR mode) construct that does not allow display constructs like lists, you probably want \fcolorbox{black}{yelow}{\begin{minipage}{.7\textwidth}\begin{question}.. Also ypu are missing % from teh ends of most of your lines in the definition which will produce bad spaces in the output – David Carlisle Jan 16 '15 at 9:45
  • Sorry for posting fragments everyone. I've created a MWE and posted at the bottom. – Aram Papazian Jan 16 '15 at 9:48
  • 2
    You know that there are packages like exsheets and classes like exam for this, right? No real need to invent the wheel once more. – Johannes_B Jan 16 '15 at 9:49
2

The problem with \fcolorbox is that does not allow lists inside it. A minipage or parbox would help in this case, but both are not breakable. I used the tcolorbox package and the tcolorbox environment to provide for a breakable box (amongst many,many other configurable options, e.g. box corners, color, title line etc.)

In addition, a package like exam or exsheets are designed for such tasks.

I used xparse's \NewDocumentCommand for 'easier' long arguments to the command instead of \newcommand.

\documentclass{article}

\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{xparse}%
\usepackage[breakable]{tcolorbox}
\newenvironment{question}{\bfseries}{\normalfont}%
\newenvironment{answer}{\itshape}{\normalfont}%

\NewDocumentCommand{\qna}{+m+m}{%
  \begin{tcolorbox}[breakable,colback=yellow]
        \begin{question}
          #1
        \end{question}
        \begin{answer}
            #2
        \end{answer}
      \end{tcolorbox}
}

\begin{document}


\qna{Here's a q}{Here an answer in list format
\begin{enumerate}
    \item answer 1
    \item answer 2
\end{enumerate}
}%

\end{document}

enter image description here

  • Although I can't use the above method due to internal reasons, it's a phenomenal answer for most use cases. Thanks =) – Aram Papazian Jan 16 '15 at 10:11
  • @AramPapazian: Thanks for accepting the answer, but why can't you use this (or a similar approach)? – user31729 Jan 16 '15 at 10:14
  • I'd need to install tcolorbox (the files aren't already included on my machine unfortunately) and so I wouldn't be able to use that package. (And yes, I could download the files and import them, but I am not allowed to on the machine I am using unfortunately) – Aram Papazian Jan 16 '15 at 14:12
  • @AramPapazian: It's always possible to install a package in a local directory of your user folder. – user31729 Jan 16 '15 at 15:14
  • 1
    @AramPapazian: Get your own machine ;-) – user31729 Jan 16 '15 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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