Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

i want write a maple book, see this example code in that book:

‎\documentclass{article}‎
‎\usepackage{tcolorbox,fancyvrb}‎
‎\tcbuselibrary{skins,breakable}‎‎
‎‎
‎‎‎‎‎‎%-------------------------------------------------- ‎Code ‎Environment‎
‎\newenvironment{‎code‎}‎
 ‎{\VerbatimEnvironment‎
  ‎\begin{tcolorbox}[‎
    ‎breakable‎,
    ‎colback=red!5!white,‎‎
    ‎colframe=red!75!‎black‎,
  ]%
  ‎\begin{Verbatim}}‎
 ‎{\end{Verbatim}\end{tcolorbox}}‎
‎
‎\newenvironment{‎outc‎‎‎}‎
 ‎{‎‎\begin{tcolorbox}[‎
    ‎breakable‎,‎
    left=0mm,right=0mm,top=0mm,bottom=0‎mm,‎
%    ‎boxsep=0mm,
    ‎colback=‎white,‎‎
    coltext=‎blue‎,‎
    ‎‎colframe=‎gray!50‎‎,
  ]‎‎‎}‎%‎
 ‎{‎\end{tcolorbox}}‎
‎
%‎\newenvironment{‎outcode‎‎}‎
% ‎{‎‎\begin{‎outc‎}‎\begin{equation}} 
% {\end{‎equation‎}‎‎‎\end{‎outc‎‎}}‎
‎\newenvironment{‎outcode‎‎}‎
 ‎{‎‎\begin{‎outc‎}‎\[‎} 
 {‎\]‎\end{‎outc‎‎}}‎
‎%-------------------------------------------------- ‎Code ‎Environment‎

‎\begin{document}‎‎
‎‎
‎In this example, we see too much space between this two environment ‎\textbf{code} and ‎\textbf{outcode}‎:‎
‎
‎‎‎‎‎\begin{‎code}‎
> ‎‎‎2+3;‎ 
‎\end{‎code‎}‎‎
‎\begin{‎‎‎outcode‎}‎
‎5‎
‎\end{‎outcode‎}‎
‎
‎\end{document}‎‎‎

output is:

enter image description here

question is: How can i write output below the maple code with two different color for background and text,and separate those with, for example \tcblower command in tcolorbox?

In tcolorbox-example.tex file in c:\texlive\2013\texmf-dist\doc\latex\tcolorbox folder, use environment tcblisting for the setting the box displays the source code and shows the output in other part below like this:

‎\begin{tcblisting}{colback=red!5!white,colframe=red!75!black}‎
‎This is a \LaTeX\ example‎:
‎\begin{equation}‎
‎\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}‎.
‎\end{equation}‎
‎\end{tcblisting}‎

output is:

enter image description here

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Instead af two separate tcolorboxes, use just one with the listing and comment option; in this way, the upper part of the box will be used for the listing and the lower part, for the "comment", which can be almost abything, in particulat, the output of the code; the bicolor skin allows you to control the color for both parts (upper and lower) independently:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tcolorbox}
\tcbuselibrary{most}

\newtcblisting{codeoutput}[1]{
breakable,
listing and comment,
bicolor,
lower separated=true,
colback=red!5,
colframe=red!75!black,
colbacklower=white,
collower=blue,
comment=#1,
overlay={
  \draw[dashed,red!80!black] (segmentation.west)--(segmentation.east);
  },
listing options={
  language={Matlab},
  aboveskip=0pt,
  belowskip=0pt,
  basicstyle=\ttfamily,
  }
}

\begin{document}

\begin{codeoutput}{5}
> 2+3; nnnnn
\end{codeoutput}

\end{document}

enter image description here

Another option:

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

\newtcblisting{codeoutput}[1]{
breakable,
listing and comment,
bicolor,
frame style={
  draw=black,
  color=Maroon
  },
colback=black,
colbacklower=Dandelion!15,
colupper=white,
collower=Maroon,
comment=#1,
listing options={
  language={Matlab},
  aboveskip=0pt,
  belowskip=0pt,
  basicstyle=\ttfamily,
  }
}

\begin{document}

\begin{codeoutput}{5}
> 2+3; nnnnn
\end{codeoutput}

\end{document}

enter image description here

share|improve this answer
    
(+1) Nice and helpful answer (as always ;-). I only removed the needless and potentially dangerous \tcblower command inside the codeoutput examples. A comment inside a tcblisting is automatically placed in the lower part when listing and comment is used. –  Thomas F. Sturm Mar 5 at 10:46
    
@ThomasF.Sturm Thanks for the edit. I must have left the \tcblower there from test codes I used when building the answer. –  Gonzalo Medina Mar 5 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.