我使用minted
包来获得带有背景颜色的语法颜色。我知道它对打印非常不友好,但我还是使用它。
我的目标是让背景矩形颜色适应羊毛页面宽度。但包含的代码必须保持在常规边距内。
因此,MWE 如下:
\documentclass[a4paper]{article}
\usepackage{minted,xcolor}
\usemintedstyle{monokai}
\definecolor{bg}{HTML}{282828} % from https://github.com/kevinsawicki/monokai
\begin{document}
{Code Example:}
\begin{minted}[bgcolor=bg,
frame=lines,
framesep=2mm]{python}
#!/usr/bin/env python2
def example_function():
return "hello world"
a = 5
b = "I am python code"
\end{minted}
\end{document}
我想要得到以下渲染:
我尝试使用和来实现这一点tcolorbox
,adjustbox
但它并没有保留文本的边距。
那么,我怎样才能获得这种渲染效果?
编辑 :
我修改了我的 MWE,如下所示:
\documentclass[11pt,letterpaper]{article}
\usepackage[breakable]{tcolorbox}
\usepackage{minted,xcolor}
\usepackage{xcolor,eso-pic}
\usepackage[savepos]{zref}
\usepackage{color}
\usepackage{lipsum}
\definecolor{bg}{HTML}{282828} % from https://github.com/kevinsawicki/monokai
\newenvironment{widthcode}%
{%
\begin{tcolorbox}[breakable,size=tight,oversize,
sharp corners,
colback=blue,
left=150pt,
right=150pt,
]}%
{%
\end{tcolorbox}
}
\setminted[text]{breaklines}
\setminted{bgcolor=bg,
linenos,
breaklines,
fontsize=\footnotesize}
\begin{document}
\begin{minted}{python}
#!/usr/bin/env python2
def example_function():
return "hello world"
a = 5
b = "I am python code"
\end{minted}
\lipsum[2]
\begin{minted}{sh}
#!/bin/sh
# Simple line count example, using sh
#
# sh tutorial: http://linuxconfig.org/Bash_scripting_Tutorial#8-2-read-file-into-bash-array
# My scripting link: http://www.macs.hw.ac.uk/~hwloidl/docs/index.html#scripting
#
# Usage: ./line_count.sh file
# -----------------------------------------------------------------------------
# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1
# remember the name of the input file
in=$1
# init
file="current_line.txt"
let count=0
# this while loop iterates over all lines of the file
while read LINE
do
# increase line counter
((count++))
# write current line to a tmp file with name $file (not needed for counting)
echo $LINE > $file
# this checks the return code of echo (not needed for writing; just for demo)
if [ $? -ne 0 ]
then echo "Error in writing to file ${file}; check its permissions!"
fi
done
echo "Number of lines: $count"
echo "The last line of the file is: `cat ${file}`"
# Note: You can achieve the same by just using the tool wc like this
echo "Expected number of lines: `wc -l $in`"
# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
mkvmerge --machin-chouette
\end{minted}
\lipsum[2]
\lipsum[2-4]
\begin{widthcode}
\begin{minted}{sh}
#!/bin/sh
# Simple line count example, using sh
#
# sh tutorial: http://linuxconfig.org/Bash_scripting_Tutorial#8-2-read-file-into-bash-array
# My scripting link: http://www.macs.hw.ac.uk/~hwloidl/docs/index.html#scripting
#
# Usage: ./line_count.sh file
# -----------------------------------------------------------------------------
# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1
# remember the name of the input file
in=$1
# init
file="current_line.txt"
let count=0
# this while loop iterates over all lines of the file
while read LINE
do
# increase line counter
((count++))
# write current line to a tmp file with name $file (not needed for counting)
echo $LINE > $file
# this checks the return code of echo (not needed for writing; just for demo)
if [ $? -ne 0 ]
then echo "Error in writing to file ${file}; check its permissions!"
fi
done
echo "Number of lines: $count"
echo "The last line of the file is: `cat ${file}`"
# Note: You can achieve the same by just using the tool wc like this
echo "Expected number of lines: `wc -l $in`"
# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
mkvmerge --machin-chouette
\end{minted}
\end{widthcode}
\end{document}
如您所见,我创建了一个widthcode
环境,从页面左侧到右侧放置了一个大的蓝色背景。这 widthcode
很好用,并且允许长文本分页:
但是,当我将一个minted
环境放入一个widthcode
环境中时,它会失败:
因此,问题变成了:如何使minted
环境在tcolorbox
环境中变得可破坏?