我想定义一个代码块宏,其用法如下:
\begin{codeblock}[bash]
# This is a comment
sudo apt-get install texlive
\end{codeblock}
甚至更好:
\codeblock[bash]{
# This is a comment
sudo apt-get install texlive
}
然而当前的示例使用多层环境:
\begin{myquote}
\begin{lstlisting}[language=Bash]
# this is a comment
sudo apt-get install texlive
\end{lstlisting}
\end{myquote}
其中myquote
使用 定义\newtcolorbox
。我知道有一个命令可以定义新环境。但是它不适用于 lstlisting。有一个命令可以定义自定义 lstenvironment,但是它不适用于 tcolorbox。
完整 Latex 示例
\documentclass[]{article}
\usepackage[utf8]{inputenc}
\title{Developers \& Operations Handbook}
\usepackage{environ}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{background}{RGB}{240,242,245}
\definecolor{primary}{RGB}{0,170,255}
\definecolor{success}{RGB}{60,220,100}
\definecolor{danger}{RGB}{240,85,110}
\definecolor{warning}{RGB}{255,170,70}
\newtcolorbox{myquote}[1][]{%
enhanced,breakable,colframe=background,
colback=background,notitle,size=fbox,
arc=0mm,outer arc=0mm,coltitle=primary,
fonttitle=\bfseries\large,boxsep=5mm,
left=0mm,right=0mm,
borderline west={1mm}{0pt}{primary},
before={\noindent},
segmentation style={solid, primary!0},
}
\begin{document}
\begin{myquote}
Normal Quotes do work
\end{myquote}
% This is how I can do code at the moment
\begin{myquote}
\begin{lstlisting}[language=Bash]
# this is a bash comment
sudo apt-get install texlive-base
\end{lstlisting}
\end{myquote}
% this is how I want do do code in the future
% \codeblock{
% # this is a bash comment
% sudo apt-get install texlive-base
% }
% == OR ==
% \begin{codeblock}[Bash]
% # this is a bash comment
% sudo apt-get install texlive-base
% \end{codeblock}
% This does not work (https://tex.stackexchange.com/questions/86705/lstlisting-in-a-newenvironment)
% \newenvironment{codeblock}[1][Bash]
% {\begin{myquote}\begin{lstlisting}[language=#1]}
% {\end{lstlisting}\end{myquote}}
% This also doesn't work
% \lstnewenvironment{codeblock}[1]
% {\begin{myquote}}}
% {\end{myquote}}
\end{document}
答案1
您始终可以使用tcblisting
具有类似以下特征的框myquote
:
\documentclass[]{article}
\usepackage[utf8]{inputenc}
\title{Developers \& Operations Handbook}
\usepackage{environ}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\definecolor{background}{RGB}{240,242,245}
\definecolor{primary}{RGB}{0,170,255}
\definecolor{success}{RGB}{60,220,100}
\definecolor{danger}{RGB}{240,85,110}
\definecolor{warning}{RGB}{255,170,70}
\newtcolorbox{myquote}[1][]{%
enhanced, breakable, colframe=background,
colback=background, notitle, size=fbox,
arc=0mm, outer arc=0mm, coltitle=primary,
fonttitle=\bfseries\large, boxsep=5mm,
left=0mm, right=0mm,
borderline west={1mm}{0pt}{primary},
before={\noindent},
segmentation style={solid, primary!0},
}
\newtcblisting{quotelst}{%
listing only,
enhanced, breakable, colframe=background,
colback=background, notitle, size=fbox,
arc=0mm, outer arc=0mm, coltitle=primary,
fonttitle=\bfseries\large, boxsep=5mm,
left=0mm, right=0mm,
borderline west={1mm}{0pt}{primary},
before={\noindent},
segmentation style={solid, primary!0},
listing options={language=Bash}
}
\begin{document}
\begin{myquote}
Normal Quotes do work
\end{myquote}
% This is how I can do code at the moment
\begin{quotelst}
# this is a bash comment
sudo apt-get install texlive-base
\end{quotelst}
\end{document}