我使用该类memoir
和包minted
并在 XeLaTeX 中进行编译。
根据问题如何让 minted 跨页但保持标题与列表末尾在一起?和打破 Minted 包中的页面,但第一题他的MWE写得不好,答案也解释得不好。第二题答案里的代码不起作用,我试着改正并计算代码。这里是:
\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}
错误:
./capitulos/wordgcp.tex:84: Undefined control sequence.
\mintcode ...= contessa, left = 10mm] \dockerfile
{#1} \tcblower \captionof ...
l.84 F
ROM php:7.2-apache
答案1
tcolorbox
定义列表的特殊框。一个例子如下打破 Minted 包中的页面我已经适应了你的代码。
环境mintedbox
有三个参数,第一个和第三个是可选的,第二个是必需的。使用第一个参数,您可以将选项添加到minted
已在 mintedbox 中声明的选项中。第二个参数是语言,第三个参数允许您为 mintedbox 的 tcolorbox 部分声明新选项。使用这个第三个参数,您可以添加一个标题,该flip title
标题带有附加到最后一个片段的选项。
您可能希望有一个“代码列表”和这些框的枚举标题、参考资料等等,但这些都是您可以逐一询问的其他问题。顺便说一句,它memoir
使用自己的字幕系统,因此captionof
没有必要。
如果以下代码不适合您,请更新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}
更新添加标签以供参考:
新的mintedbox
声明用于init options
自动枚举代码框并创建一个list of code
。现在它有四个参数,第三个参数是框的强制标题,参考标签包含在第四个可选参数中,但更改代码并将它们添加为新的强制(或可选)参数更容易。
我已添加hyperref
要使用的\autoref
命令包。
\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}
“代码”列表:
和代码框: