3

I'm using the minted, floatrow, caption, and listing packages to create code blocks like these:

my code block

To do this, I'm using a hack based on this answer. Here is what I have:

...
\usepackage{listings}
\usepackage{floatrow}
\usepackage{minted}
...

% the number style (from fancyvrb package):
\renewcommand{\theFancyVerbLine}{\textcolor{Gray}{\scriptsize\arabic{FancyVerbLine}}}

% caption style
\usepackage{caption}
\DeclareCaptionStyle{code_style}{margin=-1mm}
\DeclareCaptionFont{code_font}{\color{white}\footnotesize\bfseries}
\DeclareCaptionFormat{code_format}{\colorbox{MidnightBlue}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[listing]{%
style=code_style,format=code_format,labelfont=code_font,textfont=code_font}
% this is from floatrow package:
\floatsetup[listing]{capposition=top,rowpostcode=rule}

\makeatletter
\newenvironment{mycode}[3]
 {\VerbatimEnvironment
  \minted@resetoptions
  \setkeys{minted@opt}{linenos,fontfamily=courier,fontsize=\scriptsize, xleftmargin=21pt}
  \renewcommand{\minted@proglang}[1]{#1}
  \begin{listing}[H]
    \caption{#2}\label{#3}
      \begin{VerbatimOut}{\jobname.pyg}}
 {\end{VerbatimOut}
  \minted@pygmentize{\minted@proglang{}}
  \DeleteFile{\jobname.pyg}
  \end{listing}}
\makeatother

And I call mycode like this:

\begin{mycode}{ruby}{Código do método Project\#tasks\_by\_position (versão 3)}{code:tdd_code3}
# app/models/project.rb
def tasks_by_position position
  tasks.select { |item| item.position.to_s == position.to_s }
end
\end{mycode}

The problem is that when using the floatrow package, the [H] "put it here" placement option doesn't work, and my code is placed at the end of the text. If I remove the floatrow package, the code works fine, but I can't put the caption at the top and the line at the bottom.


edited (30/01/2012)

As egreg asked, I made an minimal example and realize that the problem is that I'm using the \documentclass{abnt} from abntex package. If I use the \documentclass{book}, for example, it works. Unfortunately, this package have only Brazilian Portuguese documentation and it's not updated since 2006.

  • 3
    It doesn't happen to me. Please, make a minimal example showing the problem. – egreg Jan 28 '12 at 10:11
  • Thanks for the code above, useful for me. For the preamble I needed to add \definecolor{MidnightBlue}{HTML}{191970} besides the basics (documentclass{...}, begin{document}, end{document}). – Jodi Schneider Aug 31 '13 at 09:49

0 Answers0