Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
\label needs a counter, since it uses the last counter value to be fed to \refstepcounter. –  Christian Hupfer Jun 21 at 6:14

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
on could avoid defining new counters and all the stuff if the question environment were a \newmdtheoremenv. –  Bernard Jun 21 at 9:41

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

share|improve this answer
    
I considered to propose that too -- in fact, my (yet-not-ready-to-be-published) package uses tcolorbox environments for question-solution behaviour. –  Christian Hupfer Jun 21 at 8:32
    
I want to use the tag label in the environment. –  Masi Jun 21 at 11:08
    
@Masi Sorry but I don't understand what you mean. –  Ignasi Jun 21 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 ;-) –  Christian Hupfer Jun 21 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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