1

I'm having some problem with the \ref command I never had. And I guess it's caused by my own style created for definition's box and similars (token from Breaking Page with Boiboites Package TikZ).

\documentclass[11pt]{book}

%I HOPE THERE'S NO MISSING PACKAGES OR CONFLICTING COMBINATIONS.

\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish, activeacute]{babel}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage{hyperref}
\usepackage[usenames, dvipsnames]{xcolor}


\usepackage{ntheorem} %Para crear estos nuevos comandos.
\usepackage{tcolorbox}
\tcbuselibrary{breakable,skins}


\newcounter{ndefi}[chapter] %Counter created for definitions.

%Creating little box for the definition.
\newcommand{\defitit}{  
    \node[fill=green!20,    
    rounded corners,        
    draw=black,             
    text=black,             
    line width=1pt,         
    inner sep=4pt,          
    anchor=west,            
    xshift=12pt]            
    at (frame.north west){\bfseries Definición \stepcounter{ndefi} \thechapter.\thendefi -.};
}

%Creating the big box for the definition.
\newtcolorbox{defi}{    
    enhanced,               
    overlay unbroken and first={\defitit},   
    colframe=green,         
    boxrule=1pt,            
    arc=3mm,                
    breakable,              
    top=15pt,               
    before=\vskip18pt,      
}


\begin{document}
\chapter{El capítulo fantasma de las cosas de prueba.}
\section{Donde las cajas de definición y demás se prueban.}

\begin{defi}[Prueba de título] \label{probando1}
    Probando, probando... 1, 2, 3...
\end{defi}

\begin{defi}\label{probando2}
    Probando, probando... 1, 2, 3...
\end{defi}

\begin{defi}
    Probando, probando... 1, 2, 3...
\end{defi} \label{probando5}

\label{probando6}\begin{defi}
    Probando, probando... 1, 2, 3...
\end{defi}


Trying the references of definitions \ref{probando1}, \ref{probando2}, \ref{probando5} and \ref{probando6}.
\end{document}

Wherever I fit the label, the reference works badly, just showing the section and referencing to it. I guess the problem is on the enviroment I created to make those boxes, but I have no idea. Or maybe the problem is I must use something different to \label or \ref...

0
0

The label has to be given as an option to the tcolorbox.

Here's a suggestion also for coping with the title.

\documentclass[11pt]{book}

\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish]{babel}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{hyperref}


%\usepackage{ntheorem} %Para crear estos nuevos comandos.
\usepackage{tcolorbox}
\tcbuselibrary{breakable,skins}


\newcounter{ndefi}[chapter] %Counter created for definitions.
% delay the redefinition because otherwise tcolorbox would override it
\AtBeginDocument{\renewcommand{\thendefi}{\thechapter.\arabic{ndefi}}}

%Creating little box for the definition.
\newcommand{\defitit}[1]{%
    \node[fill=green!20,
    rounded corners,
    draw=black,
    text=black,
    line width=1pt,
    inner sep=4pt,
    anchor=west,
    xshift=12pt]
    at (frame.north west){\bfseries Definición \thendefi.#1\unskip};
}

\providecommand{\thisdefititle}{}
%Creating the big box for the definition.
\newtcolorbox[use counter=ndefi]{defi}[1][]{
    dtitle/.store in=\thisdefititle,
    enhanced,
    overlay unbroken and first={\defitit{ \thisdefititle}},
    colframe=green,
    boxrule=1pt,
    arc=3mm,
    breakable,
    top=15pt,
    before=\vspace{18pt},
    #1,
}


\begin{document}

\chapter{El capítulo fantasma de las cosas de prueba.}
\section{Donde las cajas de definición y demás se prueban.}

\begin{defi}[dtitle=Prueba de título,label=probando1]
    Probando, probando... 1, 2, 3...
\end{defi}

\begin{defi}[label=probando2]
    Probando, probando... 1, 2, 3...
\end{defi}

\begin{defi}[label=probando3]
    Probando, probando... 1, 2, 3...
\end{defi}

\begin{defi}[label=probando4]
    Probando, probando... 1, 2, 3...
\end{defi}

Trying the references of definitions 
\ref{probando1}, \ref{probando2}, \ref{probando3} and \ref{probando4}.

\end{document}

enter image description here

I'd avoid the activeacute option that nowadays is mostly obsolete since you can directly input accented characters.

1
  • Thanks for the answer, it's been really useful and you have even fixed the issue I had to make optional names for titles. It took me a while to (more or less) understand the code and make the edition well. – DavidRomán May 23 '20 at 19:27

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.