jkuczm / mmacells

Mathematica cells in TeX
63 stars 18 forks source link

Defining custom environments based on `mmaCell` #21

Closed jkuczm closed 8 years ago

jkuczm commented 8 years ago

In #20 @Nithilher wrote:

I have one small request. I would like to set the Input Code of mathematica in borderless boxes of full width (\ŧextwidth) with a light gray as background color. However, when putting your code within \colorbox{bg}{...}, I get an error:

\definecolor{bg}{rgb}{0.95,0.95,0.95}
\colorbox{bg}{%
\begin{mmaCell}[morepattern={z, z_}]{Input}
  \mmaDef{ρ}[z_] := \mmaSub{ρ}{0} z Exp[-\mmaUnd{λ} Abs[z]]
\end{mmaCell}
}

yields

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

Is there a simple way to achive this?

jkuczm commented 8 years ago

Instead of \colorbox you can use an environment, for example tcolorbox.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=2cm]{geometry}

\usepackage{mmacells}

\usepackage{tcolorbox}

\begin{document}

\mmaSet{morefv={gobble=2}}

\mmaDefineMathReplacement{ρ}{\rho}
\mmaDefineMathReplacement{λ}{\lambda}

\begin{tcolorbox}[colback=black!5!white,sharp corners,boxrule=0pt]
\begin{mmaCell}[morepattern={z, z_}]{Input}
  \mmaDef{ρ}[z_] := \mmaSub{ρ}{0} z Exp[-\mmaUnd{λ} Abs[z]]
\end{mmaCell}
\end{tcolorbox}

\end{document}

If you want to use boxed cell more than once, you could define your own boxedMmaCell environment. Since mmaCell is a verbatim-like environment based on fancyvrbs Verbatim, we need to use \VerbatimEnvironment command when defining new mmaCell-based environments.

\newenvironment{boxedMmaCell}[2][]
  {%
    \VerbatimEnvironment%
    \begin{tcolorbox}[colback=black!5!white,sharp corners,boxrule=0pt]%
    \begin{mmaCell}[#1]{#2}%
  }
  {%
    \end{mmaCell}%
    \end{tcolorbox}%
  }

\begin{boxedMmaCell}[morepattern={z, z_}]{Input}
  \mmaDef{ρ}[z_] := \mmaSub{ρ}{0} z Exp[-\mmaUnd{λ} Abs[z]]
\end{boxedMmaCell}
Nithilher commented 8 years ago

Wow, many thanks! Works perfect.