梅威瑟:
\documentclass{article}
\usepackage{listings, tcolorbox}
\tcbuselibrary{listings,skins,theorems,xparse}
\newenvironment{mybox}
{\begin{tcolorbox}\begin{lstlisting}}
{\end{lstlisting}\end{tcolorbox}}
\begin{document}
\begin{tcolorbox}\begin{lstlisting}
This is my \LaTeX\ box.
\end{lstlisting}\end{tcolorbox}
% \begin{mybox}
% This is my \LaTeX\ box too!
% \end{mybox}
\end{document}
我的环境不起作用。也许我需要一本入门书\newenvironment
?
答案1
以下是 tcolorbox 手册第 4.13 页第 302 节的示例:
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{mybox}[2][]{%
colback=red!5!white,
colframe=red!75!black,
fonttitle=\bfseries,
title=#2,#1}
\begin{document}
\begin{mybox}[listing only]{Listing Box}
This is my \LaTeX\ box.
\end{mybox}
\bigskip
\begin{mybox}[listing side text]{Listing Box}
This is my \LaTeX\ box.
\end{mybox}
\end{document}
输出: