1

In my thesis I have defined Boxes. In these boxes I give additional background information or definitions that are too detailed for my main text. These environments are not floats, and they do not have a caption. The box number is displayed and defined in the object itself. I cannot put the boxes into a float because they no longer work when I do.

I would like to refer to the boxes in the text. Using \label{} and \ref{} does not work as it, obviously, gives the section number. I only have a limited number of boxes and only in the introduction, so I would be fine with a solution to number them manually. But I would like to keep the clickable links in the pdf-file.

Any thoughts?

PS I don't know if this is relevant but the boxes are written in subfiles that I import into my main .tex using \input{}

PPS I defined the boxes in the .cls file like this

\newcounter{mybox}[chapter] \setcounter{mybox}{0}
\renewcommand{\themybox}{\arabic{chapter}.\arabic{mybox}}
\newenvironment{mybox}[2][]{%
\refstepcounter{mybox}%
\ifstrempty{#1}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=gray!20]
{\strut Box ~\themybox};}}
}%
{\mdfsetup{%
frametitle={%
\tikz[baseline=(current bounding box.east),outer sep=0pt]
\node[anchor=east,rectangle,fill=gray!20]
{\strut Box ~\themybox:~#1};}}%
}%
\mdfsetup{innertopmargin=10pt,linecolor=gray!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=\dimexpr-\ht\strutbox\relax
}
\begin{mdframed}[]\relax%
}{\end{mdframed}}

PPPS This is what it looks like in text

\begin{mybox}[Definitions: Validity]{}
\begin{description}

 \item[Some word]  has this meaning


\end{description}
\end{mybox}

And then I tried to use \input{folder/Boxfile}\label{Box:label1}

I also tried

\begin{mybox}[Definitions: Validity]{}
\begin{description}

 \item[Some word]  has this meaning


\end{description}
\end{mybox}\label{Box:mybox}

which also refers to section numbers, not box numbers

  • 1
    Please give an example of the mybox environment in action. – Mico Oct 19 '17 at 16:59
  • 2
    Basically the \label should work since there is a \refstepcounter, but it does really matter, where the label is used there. – Christian Hupfer Oct 19 '17 at 17:00
  • Echoing @ChristianHupfer's comment, if I set up an instance of mybox via \begin{mybox}{} \label{box:hello} Hello World. \end{mybox}, I can easily cross-reference it elsewhere in the document via a \ref{box:hello} directive. Incidentally, the mybox environment is defined to take 2 arguments (the first being optional), yet the environment does not seem to make any use of the non-optional argument. Is this intentional? – Mico Oct 19 '17 at 17:05
  • This is me being sloppy. The second argument used to name "Box" but I hard-coded that and didn't remove the redundant code – Nightingale Oct 19 '17 at 17:12
  • 1
    As Mico said you have to put the label command inside your mybox environment. – Peter Wilson Oct 19 '17 at 17:22
4

The fact that the mybox environment doesn't float and doesn't feature a \caption statement is not crucial for determining whether it can be cross-referenced via LaTeX's \label-\ref mechanism. What matters is that the environment increments its associated counter via a \refstepcounter directive.

The \label directive should be provided inside the mybox environment, preferably right after the environment is entered.

Aside: Having an environment and a counter both share the exact same name (mybox) can easily give rise to confusion. How about calling the counter myboxcntr instead?

Please see the following example for how cross-referencing may be performed.

enter image description here

\documentclass{report}
\usepackage{mdframed,tikz}

\newcounter{myboxcntr}[chapter] 
\renewcommand{\themyboxcntr}{\thechapter.\arabic{myboxcntr}}
%\setcounter{myboxcntr}{0} % not needed
\newenvironment{mybox}[2][]{%
   \refstepcounter{myboxcntr}%
   \ifstrempty{#1}%
      {\mdfsetup{%
        frametitle={%
        \tikz[baseline=(current bounding box.east),outer sep=0pt]
        \node[anchor=east,rectangle,fill=gray!20]%
           {\strut Box \themyboxcntr};}}}%
       {\mdfsetup{%
        frametitle={%
        \tikz[baseline=(current bounding box.east),outer sep=0pt]
        \node[anchor=east,rectangle,fill=gray!20]%
           {\strut Box \themyboxcntr:~#1};}}}
   \mdfsetup{innertopmargin=10pt,linecolor=gray!20,%
      linewidth=2pt,topline=true,%
      frametitleaboveskip=\dimexpr-\ht\strutbox\relax}
   \begin{mdframed}[]\relax}{%
   \end{mdframed}}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}
\crefname{myboxcntr}{Box}{Boxes}

\begin{document}
\setcounter{chapter}{3} % just for this example

Yawn, big yawn, wakey-wakey, \dots
\begin{mybox}[Greetings!]{}
  \label{box:hello} % <-- crucial: provide a \label statement inside the env.
  Hello World. 
\end{mybox}

A cross-reference to \cref{box:hello}.
\end{document}
  • Many environments and their counters have the same name, e.g. equation, table or figure ;-) – Christian Hupfer Oct 19 '17 at 18:09
  • @ChristianHupfer - I know. :-) Still, quod licet Iovis (or David Carlisle...), non licet bovis, right? – Mico Oct 19 '17 at 18:11
  • Who is bovis then? ;-) – Christian Hupfer Oct 19 '17 at 18:13
  • @ChristianHupfer - Pretty much everyone except David C. and Enrico G. :-) – Mico Oct 19 '17 at 18:59
  • 1
    @Mico: That's idolatrous ;-) – Christian Hupfer Oct 20 '17 at 16:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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