4

My minimum code

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
    frametitlebackgroundcolor=green!40,
    frametitlerule=true
}
\newenvironment{question}[1]{%
    \begin{mdframed}[style=ans,frametitle={Question: #1}]
}{%
    \end{mdframed}%
}%

\begin{document}
\begin{question}
\label{masi} % PROBLEM here, since label not working!
{Question body here? What is your question?}
Answer is this and this. Lorem yea.   
\end{question}
\end{document}

which has developed from discussion but where label does not work.

How can you use label in a such environment?

  • \label needs a counter, since it uses the last counter value to be fed to \refstepcounter. –  Jun 21 '14 at 06:14

2 Answers2

5

There must be a counter for the \label command, I introduced a new counter called question. Ideally, the counter should be printed on the question too, I have added this by now and the reference works too.

\label uses the the value of the last counter, that was incremented by \refstepcounter. If there many sections in your code, it is probably convenient to use \newcounter{question}[section] in order to reset it and perhaps redefine \thequestion command to

\renewcommand{\thequestion}{\arabic{section}.\arabic{question}

As there are no sections, I have omitted that redefine.

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
  linecolor=cyan,
  backgroundcolor=yellow!20,
    frametitlebackgroundcolor=green!40,
    frametitlerule=true
}

\newcounter{question}[section]%
\setcounter{question}{0}

\newenvironment{question}[1]{%
\refstepcounter{question}%
    \begin{mdframed}[style=ans,frametitle={Question \thequestion: #1}]
}{%
    \end{mdframed}%
}%

\begin{document}
\begin{question}
 % PROBLEM here, since label not working!
{Question body here? What is your question?}
\label{masi}
Answer is this and this. Lorem yea.   
\end{question}

\begin{question}
 % PROBLEM here, since label not working!
{Question body here? What is your question?}
\label{masi2}
Answer is this also. Lorem yea.   
\end{question}


The Question \ref{masi} was really tricky, but Question \ref{masi2} was really tricky ;-)




\end{document}

enter image description here

  • on could avoid defining new counters and all the stuff if the question environment were a \newmdtheoremenv. – Bernard Jun 21 '14 at 09:41
4

May be you can consider tcolorbox. It offers a theorems library which simplifies definition and use of enumerated colorfull boxes.

The macro

\newtcbtheorem[<init options>]{<name>}{<display name>}{<options>}{<prefix>}

defines a new name enumerated environment (no worry about counters the package take care of them). display name will appear before number, options define environment style and prefix will be internally added as prefix to all used labels. After definition, in your text you can use

\begin{name}{Some description text}{label}
....
\end{name}

This create a boxed environment with Some description text in title box, ... in lower box and with prefix:label as label for further reference. Here you have a little example

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{
    questionstyle/.style={enhanced, colback=white, colframe=blue!20, arc=0pt, 
                                 fonttitle=\bfseries, colbacktitle=green!40,coltitle=black, 
                                 boxed title style={arc=0pt}}
    }

\newtcbtheorem[]{myquestion}{Question}{questionstyle}{quest}

\begin{document}

\begin{myquestion}{Question Body goes here. As you can see this is a very long question. At least its title is. What's your question?}{masi}
This is my answer
\end{myquestion}

\begin{myquestion}{New question related to question \ref{quest:masi}}{masi2}
This is the answer to question \ref{quest:masi2} which was related with question \ref{quest:masi}
\end{myquestion}
\end{document}

enter image description here

Ignasi
  • 136,588
  • I considered to propose that too -- in fact, my (yet-not-ready-to-be-published) package uses tcolorbox environments for question-solution behaviour. –  Jun 21 '14 at 08:32
  • I want to use the tag label in the environment. – Léo Léopold Hertz 준영 Jun 21 '14 at 11:08
  • @Masi Sorry but I don't understand what you mean. – Ignasi Jun 21 '14 at 12:17
  • 1
    @Masi: The label is contained in the 2nd argument, which is basically the same as writing it yourself within the environment body, apart from being shorter ;-) –  Jun 21 '14 at 14:23