1

I use the class memoir and the packages minted and compile in XeLaTeX.

Based on the questions How to let minted break across pages but keep the caption together with the end of listing? and Breaking pages in minted package, but in the first question, his MWE is poorly written and the answer is also poorly explained. In the second question, in the answer, the codes did not work, I tried to correct and figure the codes. Here is:

\documentclass{memoir}
\usepackage{minted}
\usepackage{setspace}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}

\definecolor{contessa}{HTML}{BF616A}

\newmintedfile{docker}
{
  baselinestretch = 1.2,
  breakanywhere,
  breaklines,
  framesep = 2mm,
  frame = single,
  lineos = false,
  mathescape,
  numbersep = 16pt,
  tabsize = 4
}

\newenvironment{mintcode}[4]
{
    \label{code:#2}
    \begin{tcolorbox}[breakable, colback = contessa, left = 10mm]
        \dockerfile{#1}
        \tcblower
        \captionof{code}[#3]{#4}
    \end{tcolorbox}
}

\begin{document}

Here is the \autoref{lst:dockerfile2}

\begin{mintcode}{docker}{lst:dockerfile2}{Exemplo de códigos no \texttt{Dockerfile}}
FROM php:7.2-apache

# instalar as extensões PHP que precisas
RUN set -ex; \
  \
  savedAptMark="$(apt-mark showmanual)"; \
  \
  apt-get update; \
  apt-get install -y --no-install-recommends \
    libjpeg-dev \
    libpng-dev \
  ; \
  \
  docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
  docker-php-ext-install gd mysqli opcache zip; \
  \
# restaurar a lista do «manual» do apt-mark, pois esse "purge --auto-remove" removerá todas as dependências de construição
  apt-mark auto '.*' > /dev/null; \
  apt-mark manual $savedAptMark; \
  ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
    | awk '/=>/ { print $3 }' \
    | sort -u \
    | xargs -r dpkg-query -S \
    | cut -d: -f1 \
    | sort -u \
    | xargs -rt apt-mark manual; \
  \
  apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
  rm -rf /var/lib/apt/lists/*

# configurar as configurações recomendadas do PHP.ini
# veja https://secure.php.net/manual/en/opcache.installation.php
RUN { \
    echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=8'; \
    echo 'opcache.max_accelerated_files=4000'; \
    echo 'opcache.revalidate_freq=2'; \
    echo 'opcache.fast_shutdown=1'; \
    echo 'opcache.enable_cli=1'; \
  } > /usr/local/etc/php/conf.d/opcache-recommended.ini

RUN a2enmod rewrite expires

VOLUME /var/www/html

ENV WORDPRESS_VERSION 4.9.8
ENV WORDPRESS_SHA1 0945bab959cba127531dceb2c4fed81770812b4f

RUN set -ex; \
  curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
  echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
# os tarballs de upstream incluem ./wordpress/, então isto nos dará /usr/src/wordpress
  tar -xzf wordpress.tar.gz -C /usr/src/; \
  rm wordpress.tar.gz; \
  chown -R www-data:www-data /usr/src/wordpress

COPY docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
\end{mintcode}

\end{document}

Error:

./capitulos/wordgcp.tex:84: Undefined control sequence.
\mintcode ...= contessa, left = 10mm] \dockerfile 
                                                  {#1} \tcblower \captionof ...
l.84 F
      ROM php:7.2-apache
2

tcolorbox defines special boxes for listings. One example is shown in Breaking pages in minted package and I've adapted to your code.

The mintedbox environment has three parameters, first and third are optional and second mandatory. With first parameter you can add options to minted options already declared inside the mintedbox. Second parameter is the languaje and third parameter allows you to declare new options for the tcolorbox part of mintedbox. With this third parameter you can add a title which with flip title option is attached to last fragment.

You probably want to have a "list of codes" and enumerated captions for these boxes, and references, and ... but this are other questions which you can ask one by one. By the way, memoir uses its own captioning system so captionof is not necessary.

In case following code doesn't work for you, update tcolorbox.

\documentclass{memoir}
\usepackage{minted}
\usepackage{setspace}
%\usepackage{xcolor}  %<- already loaded by tcolorbox
\usepackage[most, minted]{tcolorbox}
%\tcbuselibrary{breakable} %<- loaded with `most` option

\definecolor{contessa}{HTML}{BF616A}

\DeclareTCBListing{mintedbox}{O{}mO{}}{%
enhanced,
  listing only,
  breakable,
  minted language=#2,
  minted options={%
    linenos,
    numbersep=5pt,
    gobble=0,
    tabsize=4,
    breaklines=true,
    mathescape,
    framesep=2mm,#1},
  % Appearance tweaks
  top=0pt,
  bottom=0pt,
  left=0pt,
  right=0pt,
  arc=0pt,
  colframe=contessa,
  colback=contessa!10,
  coltitle=contessa!30!black,
  fonttitle=\bfseries,
  colbacktitle=contessa!40,
  flip title,
  #3}

\begin{document}

Here is the %\autoref{lst:dockerfile2}

\begin{mintedbox}{docker}[title=Exemplo de códigos no \texttt{Dockerfile}]
FROM php:7.2-apache

# instalar as extensões PHP que precisas
RUN set -ex; \
  \
  savedAptMark="$(apt-mark showmanual)"; \
  \
  apt-get update; \
  apt-get install -y --no-install-recommends \
    libjpeg-dev \
    libpng-dev \
  ; \
  \
  docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
  docker-php-ext-install gd mysqli opcache zip; \
  \
# restaurar a lista do «manual» do apt-mark, pois esse "purge --auto-remove" removerá todas as dependências de construição
  apt-mark auto '.*' > /dev/null; \
  apt-mark manual $savedAptMark; \
  ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
    | awk '/=>/ { print $3 }' \
    | sort -u \
    | xargs -r dpkg-query -S \
    | cut -d: -f1 \
    | sort -u \
    | xargs -rt apt-mark manual; \
  \
  apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
  rm -rf /var/lib/apt/lists/*

# configurar as configurações recomendadas do PHP.ini
# veja https://secure.php.net/manual/en/opcache.installation.php
RUN { \
    echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=8'; \
    echo 'opcache.max_accelerated_files=4000'; \
    echo 'opcache.revalidate_freq=2'; \
    echo 'opcache.fast_shutdown=1'; \
    echo 'opcache.enable_cli=1'; \
  } > /usr/local/etc/php/conf.d/opcache-recommended.ini

RUN a2enmod rewrite expires

VOLUME /var/www/html

ENV WORDPRESS_VERSION 4.9.8
ENV WORDPRESS_SHA1 0945bab959cba127531dceb2c4fed81770812b4f

RUN set -ex; \
  curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
  echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
# os tarballs de upstream incluem ./wordpress/, então isto nos dará /usr/src/wordpress
  tar -xzf wordpress.tar.gz -C /usr/src/; \
  rm wordpress.tar.gz; \
  chown -R www-data:www-data /usr/src/wordpress

COPY docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
\end{mintedbox}

\end{document}

enter image description here

Update Addition of labels for reference:

The new mintedbox declaration uses init options to automatically enumerate the code boxes and create a list of code. Now it has four parameters, the third one is a mandatory title for the box, labels for reference are included in fourth optional parameter, but it's easier to change the code and add them as a new mandatory (or optional) parameter.

I've added hyperref package to use \autoref command.

\documentclass{memoir}
\usepackage{minted}
\usepackage{setspace}
%\usepackage{xcolor}  %<- already loaded by tcolorbox
\usepackage[most, minted]{tcolorbox}
%\tcbuselibrary{breakable} %<- loaded with `most` option
\usepackage{hyperref}

\definecolor{contessa}{HTML}{BF616A}

\DeclareTCBListing[%
    auto counter, 
    number within=chapter,
    list inside=code]%
    {mintedbox}%
    {O{}mmO{}}{%
    enhanced,
    listing only,
    breakable,
    minted language=#2,
    minted options={%
        linenos,
        numbersep=5pt,
        gobble=0,
        tabsize=4,
        breaklines=true,
        mathescape,
        framesep=2mm,#1},
  % Appearance tweaks
  top=0pt,
  bottom=0pt,
  left=0pt,
  right=0pt,
  arc=0pt,
  colframe=contessa,
  colback=contessa!10,
  coltitle=contessa!30!black,
  fonttitle=\bfseries,
  colbacktitle=contessa!40,
  flip title,
  title=Code~\thetcbcounter: #3,
  list text=#3,
  #4}

\begin{document}


\tcblistof[\chapter]{code}{List of codes}

\chapter{Some code}



Here is the \autoref{code:lst:dockerfile2} and some more:

\begin{mintedbox}{docker}{Exemplo de códigos no \texttt{Dockerfile}}[label=code:lst:dockerfile2]
FROM php:7.2-apache

# instalar as extensões PHP que precisas
RUN set -ex; \
  \
  savedAptMark="$(apt-mark showmanual)"; \
  \
  apt-get update; \
  apt-get install -y --no-install-recommends \
    libjpeg-dev \
    libpng-dev \
  ; \
  \
\end{mintedbox}

\begin{mintedbox}{docker}{Exemplo de códigos no \texttt{Dockerfile}}[label=code:lst:dockerfile3]
  docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
  docker-php-ext-install gd mysqli opcache zip; \
  \
# restaurar a lista do «manual» do apt-mark, pois esse "purge --auto-remove" removerá todas as dependências de construição
  apt-mark auto '.*' > /dev/null; \
  apt-mark manual $savedAptMark; \
  ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
    | awk '/=>/ { print $3 }' \
    | sort -u \
    | xargs -r dpkg-query -S \
    | cut -d: -f1 \
    | sort -u \
    | xargs -rt apt-mark manual; \
  \
  apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
  rm -rf /var/lib/apt/lists/*
\end{mintedbox}

\begin{mintedbox}{docker}{Exemplo de códigos no \texttt{Dockerfile}}[label=code:lst:dockerfile4]
# configurar as configurações recomendadas do PHP.ini
# veja https://secure.php.net/manual/en/opcache.installation.php
RUN { \
    echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=8'; \
    echo 'opcache.max_accelerated_files=4000'; \
    echo 'opcache.revalidate_freq=2'; \
    echo 'opcache.fast_shutdown=1'; \
    echo 'opcache.enable_cli=1'; \
  } > /usr/local/etc/php/conf.d/opcache-recommended.ini

RUN a2enmod rewrite expires

VOLUME /var/www/html

ENV WORDPRESS_VERSION 4.9.8
ENV WORDPRESS_SHA1 0945bab959cba127531dceb2c4fed81770812b4f

RUN set -ex; \
  curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
  echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
# os tarballs de upstream incluem ./wordpress/, então isto nos dará /usr/src/wordpress
  tar -xzf wordpress.tar.gz -C /usr/src/; \
  rm wordpress.tar.gz; \
  chown -R www-data:www-data /usr/src/wordpress

COPY docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
\end{mintedbox}

\end{document}

The list of "codes":

enter image description here

and the code boxes:

enter image description here

  • Does it not support \label` for \autoref`? :-( – Gustavo Reis Nov 29 '18 at 10:30
  • @GustavoReis What does \autoref? I commented out because it gave me an error in your MWE. Of course, tcolorboxes can be enumerated and labelled and referenced but this is not mentioned in your question. – Ignasi Nov 29 '18 at 10:40
  • Sorry for not having explained that I wanted to include the label anr the reference to be called by \autoref. The command \autoref will forward and refer you to see the figure or the code. Does the tcolorbox will automatically add it to the list of codes? What is the similar to \autoref for tcolorbox? – Gustavo Reis Nov 29 '18 at 11:23
  • @GustavoReis I've updated my answer to include labels and autonumbering. Hope it's better now. – Ignasi Nov 29 '18 at 21:02

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.