tcblisting 中的图形

tcblisting 中的图形

我正在使用包 tcolorbox 创建一个包含示例 Latex 的文件,我已经查看了 tcblisting 环境选项并使用了这些:

  • 列表外的文本
  • 列出外部文本
  • 列出上面的文字
  • 列表上方的文字

但有时我想展示的示例并不符合我的期望,于是我就有了疑问:您可以使用选项来添加图形而不是格式化的文本吗?类似于“showexpl”包中的“graphic”选项。问候,Pablo 编辑:像这样 在此处输入图片描述

答案1

从 开始tcolorbox version 2.71 (2014/02/21),所有缺失的comment outside listingea 选项都已实现。评论可以视为任何东西,尤其是图片。对于图片,有一个新选项image comment,可以简单地将图片设置为评论。

A评论 外部tcolorbox左/右/下/上)始终被视为降低框内容的一部分。因此,在下面的例子中,center lower将位于列表上方的图片置于中心。

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcblisting{mylisting}[1]{%
  colback=red!5!white,colframe=red!75!black,
  image comment={width=2.5cm}{example-image-a.pdf},
  #1}

\begin{document}


\section{listing outside image}

\begin{mylisting}{listing outside comment,righthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{image outside listing}

\begin{mylisting}{comment outside listing,lefthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{listing above image}

\begin{mylisting}{listing above comment,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{image above listing}

\begin{mylisting}{comment above listing,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\clearpage
\section{listing side image}

\begin{mylisting}{listing side comment,lower separated=false,
  righthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{image side listing}

\begin{mylisting}{comment side listing,lower separated=false,
  lefthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{listing and image}

\begin{mylisting}{listing and comment,bicolor,colbacklower=white,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{image and listing}

\begin{mylisting}{comment and listing,bicolor,colback=white,
  colbacklower=red!5!white,center upper}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\end{document}

在此处输入图片描述 在此处输入图片描述

答案2

重要提示

据软件包作者称,自 2.71 版起tcolorbox,列表和的代码更改了几个内部命令。用户界面保持不变,但下面带有内部宏的示例代码可能不再起作用。无论如何,从 2.71 版开始,下面的命令不再需要,因为缺少的选项已合并到软件包中。

在下面的示例代码中,我定义了四个新选项,允许插入与某些列表相对应的现有图像;这四个选项是

listing above image
image above listing
listing outside image
image outside listing

第一个是将代码放在图像上方;第二个是将图像放在代码上方;第三个和第四个是将代码与图像并排放置(列表在右边,图像在左边,反之亦然)。

\tcbuselistingimage命令用于声明图像;可选参数将传递给 的可选参数\includegraphics,强制参数接收图像文件的名称。

代码:

\documentclass[dvipsnames]{article}
\usepackage{tcolorbox}
\usepackage{bera}
\tcbuselibrary{most}

\definecolor{mygray}{RGB}{238,238,238}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{myblue}{RGB}{20,105,176}

\lstdefinestyle{mystyle}{language=[LaTeX]Tex,
    basicstyle=\normalfont\ttfamily,
    texcsstyle=*\color{keywords},
    stepnumber=1,
    showstringspaces=false,
    breaklines=true,
    moretexcs={tikz,node},
    literate=
          *{\{}{{\textcolor{myblue}{\{}}}{1}
            {\}}{{\textcolor{myblue}{\}}}}{1}
            {[}{{\textcolor{myblue}{[}}}{1}
            {]}{{\textcolor{myblue}{]}}}{1}
}

\makeatletter
\newcommand\tcbuselistingimage[2][]{%
  \def\@tcbuselistingimage{\includegraphics[#1]{#2}}}%

\def\tcb@inputlisting@out@listingAndText{%
  \tcb@inputlisting@out@prepare%
  \tcbdimto\tcb@w@outside{\tcb@w@lower}%
  \tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@lower-\kvtcb@sbs@gap}%
  \begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
  \tcb@minipage{\tcb@w@inside}%
  \begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
  \endminipage\hfill%
  \iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
    \begingroup\tcb@layer@inc\tcbuselistingtext\tcb@layer@dec\endgroup%
    \unskip\kvtcb@after@lower\endminipage\fi%
  \end{minipage}\end{tcb@sbsfloat}%
}

\def\tcb@inputlisting@out@listingAboveImage{%
  \begin{tcolorbox}[after=]\tcbuselistinglisting\end{tcolorbox}
  \iftcb@lowervisible\par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
    \tcb@minipage{\kvtcb@width}\centering\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
     \begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
     \unskip\kvtcb@after@lower\endminipage\fi%
  \kvtcb@afterbox%
}
\def\tcb@inputlisting@out@ImageAboveListing{%
  \kvtcb@beforebox\noindent%
  \iftcb@lowervisible\tcb@minipage{\kvtcb@width}\centering\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
     \begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
     \unskip\kvtcb@after@lower\endminipage%
     \par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
     \fi%
  \begin{tcolorbox}[before=]\tcbuselistinglisting\end{tcolorbox}%
}

\def\tcb@inputlisting@out@listingAndImage{%
  \tcb@inputlisting@out@prepare%
  \tcbdimto\tcb@w@outside{\tcb@w@lower}%
  \tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@lower-\kvtcb@sbs@gap}%
  \begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
  \tcb@minipage{\tcb@w@inside}%
  \begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
  \endminipage\hfill%
  \iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
    \begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
    \unskip\kvtcb@after@lower\endminipage\fi%
  \end{minipage}\end{tcb@sbsfloat}%
}

\def\tcb@inputlisting@out@imageAndListing{%
  \tcb@inputlisting@out@prepare%
  \tcbdimto\tcb@w@outside{\tcb@w@upper}%
  \tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@upper-\kvtcb@sbs@gap}%
  \begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
  \iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
    \begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
    \unskip\kvtcb@after@lower\endminipage\fi\hfill%
  \tcb@minipage{\tcb@w@inside}%
  \begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
  \endminipage%
  \end{minipage}\end{tcb@sbsfloat}%
}

\tcbset{%
  listing file/.store in=\kvtcb@listingfile,%
  comment/.store in=\kvtcb@listingcomment,%
  listing and text/.code={\tcb@set@listing@process{\tcb@listing@listingAndText}},%
  text and listing/.code={\tcb@set@listing@process{\tcb@listing@textAndListing}},%
  listing and comment/.code={\tcb@set@listing@process{\tcb@listing@listingAndComment}},%
  comment and listing/.code={\tcb@set@listing@process{\tcb@listing@commentAndListing}},%
  listing only/.code={\tcb@set@listing@process{\tcb@listing@listing}},%
  text only/.code={\tcb@set@listing@process{\tcbuselistingtext}},%
  text above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAboveListing},%
  image above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@ImageAboveListing},%
  listing above text/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAboveText},%
  listing above image/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAboveImage},%
  text side listing/.style={sidebyside,text and listing},%
  listing side text/.style={sidebyside,listing and text},%
  text outside listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAndListing},%
  image outside listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@imageAndListing},%
  listing outside text/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAndText},%
  listing outside image/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAndImage},%
  listing engine/.is choice,
}
\makeatother

\newtcblisting{ltxlisting}[1][]{
  skin=bicolor,
  boxrule=1mm,
  fonttitle=\bfseries,
  coltitle=black,
  frame style={
    draw=black,
    color=Aquamarine,
  },
  colback=mygray,
  listing options={style=mystyle},
  #1
}


\begin{document}

\tcbuselistingimage[width=4cm]{codei}
\begin{ltxlisting}[listing above image]
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{ltxlisting}

\begin{ltxlisting}[listing outside image]
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{ltxlisting}

\tcbuselistingimage[width=5cm]{codeii}
\begin{ltxlisting}[image above listing]
\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikz\node[fill=red!30] {A Ti\textit{k}Z node};

\end{document}
\end{ltxlisting}

\begin{ltxlisting}[image outside listing]
\documentclass{article}
\usepackage{tikz}

\begin{document}

\tikz\node[fill=red!30] {A Ti\textit{k}Z node};

\end{document}
\end{ltxlisting}

\end{document}

在此处输入图片描述

相关内容