LaTeX already told you the reason:
Package multicol Warning: Floats and marginpars not allowed inside `multicols' environment!.
And the entire float object is gone including the \label
. Therefore the reference is undefined.
Also H
is not supported causing an error, which is fixed by adding package float
. If you don't want a floating object, then there are other means to add a caption, see the following example.
Also if there are two columns, then there is no place for an image with width of the whole \textwidth
. \linewidth
is better, because it reflects the current line width.
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{enumerate}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
blahblah in figure\ref{diagram1}.
\noindent
\begin{minipage}{\linewidth}
\centering
\includegraphics[width=\linewidth]{images/diagram.png}
\captionof{figure}{blahblahblah }
\label{diagram1}
\end{minipage}
\end{multicols}
\end{document}
If you want to have a figure across the full text width, then figure*
instead of figure
can be used, but only as floating object without H
:
\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[demo]{graphicx}
\usepackage{enumerate}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
blahblah in figure\ref{diagram1}.
\begin{figure*}
\includegraphics[width=\linewidth]{images/diagram.png}
\caption{blahblahblah }
\label{diagram1}
\end{figure*}
\end{multicols}
\end{document}
You need to use label=...
option to the self-defined tcolorbox
, therefore I added an optional argument to the vaja
box.
If cleveref
is used, say crefname=...
in the init options in order to provide for the correct cleveref
reference names.
Explanation:
tcolorbox
has a phantom=
key -- every argument to phantom
will added to a list and stored in a \sbox
macro -- this is true for the \refstepcounter
macro for the internal counter of the tcolorbox
as well, such that the redefined \@currentlabel
gets lost. Any \label
outside of the same phantom=...
key command will not grab the correct \@currentlabel
then.
The label=...
option however places \label
directly after the \refstepcounter
in the phantom 'group' → the \@currentlabel
is stored correctly and 'survives'.
\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}
\newcounter{task}
\newtcolorbox[use counter=task,
number within=section,
number freestyle={\thesection-\noexpand\arabic{\tcbcounter}},
]{vaja}[1][]%
{enhanced jigsaw,
breakable,
toggle left and right,
boxrule=0mm,
colback=white, colframe=red,
top=0mm,bottom=1mm,left=1mm,right=1mm,
arc=0mm,
borderline={0.5pt}{0pt}{red, sharp corners},
fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
rightrule=1mm,
overlay={%
\tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
below right=0mm and \marginparsep] at (frame.north east) {Vaja
\thetcbcounter};}
{\node[font=\large\sffamily, text=red, inner sep=0mm,
below left=0mm and \marginparsep] at (frame.north west) {Vaja
\thetcbcounter};}
},%end of overlay
#1
}% end of task
\usepackage{lipsum}
\begin{document}
\begin{vaja}[label=task:ce-1]% label hasn't any effect
\lipsum*[11]
\end{vaja}
\newpage
\begin{vaja}[label={task:ce-2}]% label hasn't any effect
\lipsum[11]
see \ref{task:ce-1}
\end{vaja}
see \ref{task:ce-1} and \ref{task:ce-2}% doesn't work
\end{document}
Best Answer
Without using any packages you could use inbuilt theorem environment