Going against what I thought it was, the problem was the hyperref package. As this other post here hinted, the include's order was bogus, but also I had to add a \phantomsection
.
So, my preamble was
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
% color
\usepackage[table,dvipsnames]{xcolor}
% Blue links
\usepackage[unicode,colorlinks=true,linkcolor=blue]{hyperref}
% "Natbib" style for references
\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp]{biblatex}
% \makeglossaries
% http://en.wikibooks.org/wiki/LaTeX/Glossary#Using_defined_terms
\usepackage[xindy,toc]{glossaries}
% http://texblog.org/tag/addcontentsline/
\usepackage[numbib]{tocbibind}
% \newminted{cpp}
% [chapter] to number by chapter
\usepackage[chapter]{minted}
% TODO anotations
\usepackage[colorinlistoftodos,portuguese]{todonotes}
\usepackage{float}
\usepackage{caption} % \begin{caption}
\usepackage{csquotes} % required by {babel}
\usepackage{graphicx} % \includegraphics
\usepackage{subcaption} % \begin{subfigure}
\usepackage{verbatim} % \begin{comment}
\usepackage{fullpage} % thinner margins
\usepackage{indentfirst} % indent 1st paragraphs
\usepackage{setspace} % \setstretch
\usepackage{afterpage} % \afterpage https://tex.stackexchange.com/q/88657
% turning 'fi' ligatures off
% http://www.latex-community.org/forum/viewtopic.php?f=5&t=953#p13896
\usepackage{microtype}
\DisableLigatures{encoding = *, family = *}
Changing the include's order, turned into
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
% color
\usepackage[table,dvipsnames]{xcolor}
\usepackage{caption} % \begin{caption}
\usepackage{csquotes} % required by {babel}
\usepackage{graphicx} % \includegraphics
\usepackage{subcaption} % \begin{subfigure}
\usepackage{verbatim} % \begin{comment}
\usepackage{fullpage} % thinner margins
\usepackage{indentfirst} % indent 1st paragraphs
\usepackage{setspace} % \setstretch
\usepackage{afterpage} % \afterpage https://tex.stackexchange.com/q/88657
% \newminted{cpp}
% [chapter] to number by chapter
\usepackage[chapter]{minted}
% Blue links
\usepackage[unicode,colorlinks=true,linkcolor=blue]{hyperref}
% turning 'fi' ligatures off
% http://www.latex-community.org/forum/viewtopic.php?f=5&t=953#p13896
\usepackage{microtype}
\DisableLigatures{encoding = *, family = *}
% "Natbib" style for references
\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp]{biblatex}
% \makeglossaries
% http://en.wikibooks.org/wiki/LaTeX/Glossary#Using_defined_terms
\usepackage[xindy,toc]{glossaries}
% http://texblog.org/tag/addcontentsline/
\usepackage[numbib]{tocbibind}
% TODO anotations
\usepackage[colorinlistoftodos,portuguese]{todonotes}
and the renew command for \listoflistings
with the \phantomsection
% https://tex.stackexchange.com/a/99656/5125
\renewcommand{\listoflistings}{%
\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\listoflistingscaption}%
\listof{listing}{\listoflistingscaption}%
}
Now, clicking the list of listings entry on table of contents guides to the right page of the listing.
You can define a non-floating environment which can break over pages and put the minted
environment in that one. This is a documented feature:
8 FAQ and Troubleshooting
- I need a listing environment that supports page breaks. The built-in listing environment is a standard float; it doesn’t
support page breaks. You will probably want to define a new
environment for long floats. [...]
With the caption package, it
is best to use minted’s newfloat package option. See
https://tex.stackexchange.com/a/53540/10742
Loading minted.sty
with newfloat
option enables you to customize the listing
environment, caption.sty
offers \captionof
for captions outside floating environments. Check their docs for further (plenty of) options.
Putting it together:
\documentclass{article}
\usepackage[newfloat]{minted}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Source Code}
\begin{document}
\begin{code}
\captionof{listing}{My C-Code}
\label{code:c-code}
\begin{minted}{c}
int main() {
printf("hello, world");
return 0;
}
\end{minted}
\end{code}
Reference to \ref{code:c-code}.
\end{document}
![enter image description here](https://i.stack.imgur.com/0mqTD.png)
Best Answer
The
floatrow
package will do the trick, but it must be loaded beforeminted
to prevent it from loading thefloat
package.