输出和代码在同一张表上

输出和代码在同一张表上

我正在考虑创建一个查询材料,以便我可以看到下面代码的编译结果。

由于我刚开始接触 TEX,因此我不想在系统中积累多个 TEX 文件。我宁愿创建一个包含我正在学习的所有内容的 PDF。

有人建议我使用该tcolorbox软件包,但我认为我做的没有正确。

我尝试编译下面的代码,但是没有成功。

\documentclass{article}
\usepackage{tcolorbox}
\usepackage[listings]{tcolorbox}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview} \PreviewEnvironment{tikzpicture} \setlength{\PreviewBorder}{10pt}%
%%%>
\usetikzlibrary{calc}
\usepackage{amssymb}

%----------------------------------------------------------------------------------------
%   Definition in the preamble
%----------------------------------------------------------------------------------------
\tcbset{
texexp/.style={colframe=red!50!yellow!50!black, colback=red!50!yellow!5!white,
coltitle=red!50!yellow!3!white,
fonttitle=\small\sffamily\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Example \thetcbcounter: #1},label={#2}},
}
\newtcblisting{texexp}[1]{texexp,#1}
\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
example={#2}{#3},#1}
\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}

\begin{document}

\begin{tikzpicture}
\draw[dashed,color=gray] (0,0) arc (-90:90:0.5 and 1.5);
\draw[semithick] (0,0) -- (4,1);
\draw[semithick] (0,3) -- (4,2);
\draw[semithick] (0,0) arc (270:90:0.5 and 1.5); \draw[semithick] (4,1.5) ellipse (0.166 and 0.5);
\draw (-1,1.5) node {$\varnothing d_1$};
\draw (3.3,1.5) node {$\varnothing d_2$};
\draw[|-,semithick] (0,-0.5) -- (4,-0.5);
\draw[|->,semithick] (4,-0.5) -- (4.5,-0.5);
\draw (0,-1) node {$x=0$};
\draw (4,-1) node {$x=l$};
\end{tikzpicture}

\end{document}

我想要创建的 PDF 模板的下方:

在此处输入图片描述

编辑

在以下帮助下,我能够取得进一步的进步:克里斯蒂安·哈普弗

我的尝试:

\documentclass{article}

\usepackage[most]{tcolorbox}
\begin{document}

\begin{tcblisting}{% Some options here
}
\newcommand{\codewithoutput}[1]
{
\begin{tikzpicture}
\draw[dashed,color=gray] (0,0) arc (-90:90:0.5 and 1.5);
\draw[semithick] (0,0) -- (4,1);
\draw[semithick] (0,3) -- (4,2);
\draw[semithick] (0,0) arc (270:90:0.5 and 1.5); \draw[semithick] (4,1.5) ellipse (0.166 and 0.5);
\draw (-1,1.5) node {$\varnothing d_1$};
\draw (3.3,1.5) node {$\varnothing d_2$};
\draw[|-,semithick] (0,-0.5) -- (4,-0.5);
\draw[|->,semithick] (4,-0.5) -- (4.5,-0.5);
\draw (0,-1) node {$x=0$};
\draw (4,-1) node {$x=l$};
\end{tikzpicture}
}
\codewithoutput{\LaTeX}
\end{tcblisting}
\end{document}

在此处输入图片描述

答案1

通过Christian Hupfer 的评论:

\documentclass{article}

\usepackage[most]{tcolorbox}

\begin{document}

\begin{tcolorbox}

\begin{tcblisting}{colback=red!5!white,colframe=red!75!black,title=TRONCO DE CONE}

\begin{tikzpicture}
\draw[dashed,color=gray] (0,0) arc (-90:90:0.5 and 1.5);
\draw[semithick] (0,0) -- (4,1);
\draw[semithick] (0,3) -- (4,2);
\draw[semithick] (0,0) arc (270:90:0.5 and 1.5); \draw[semithick]
(4,1.5) ellipse (0.166 and 0.5);
\draw (-1,1.5) node {$\varnothing d_1$};
\draw (3.3,1.5) node {$\varnothing d_2$};
\draw[|-,semithick] (0,-0.5) -- (4,-0.5);
\draw[|->,semithick] (4,-0.5) -- (4.5,-0.5);
\draw (0,-1) node {$x=0$};
\draw (4,-1) node {$x=l$};
\end{tikzpicture}

\end{tcblisting}

\end{tcolorbox}

\end{document}

在此处输入图片描述

相关内容