jkuczm / mmacells

Mathematica cells in TeX
61 stars 18 forks source link

Support for Images and Boxes #39

Open abduld opened 4 years ago

abduld commented 4 years ago

Hi, I have been a very happy user for this package and I am starting to use it for more advanced constructs. I am wondering how one is able to represent

image

Using the mmacells package.

Thank you

jkuczm commented 4 years ago

Images can be included in cells using \mmaGraphics command, which takes one argument - a filename that will be passed to \includegraphics.

As to boxes, shown in above picture, there are currently no builtin commands to deal with them. One could play a bit with tcolorbox package to get something similar.

Putting it together we can get something like this:

\documentclass{article}

\usepackage{lmodern}
\usepackage{upquote}
\usepackage{tcolorbox}
\usepackage{mmacells}

\newcommand{\myRule}{%
  \textcolor{black!10}{\rule[-.5\baselineskip]{.5mm}{1.5\baselineskip}}%
}
\newtcbox{\myBox}[1][]{
  nobeforeafter,
  tcbox raise base,
  boxrule=.2mm,
  boxsep=0mm,
  colframe=black!10,#1
}
% Similar command probably should be available in package.
\newcommand{\mmaTrunc}[1]{%
  \myBox[
    colback=blue!4,
    left=1mm,
    right=1mm,
    top=4mm,
    bottom=0mm,
  ]{\begin{tabular}{l}
    #1\vspace{2.5ex}\\
    \footnotesize \color{black!50}
    large output \myRule{}
    \bfseries \color[rgb]{.3,.4,.5}
    show less \myRule{}
    show more \myRule{}
    show all \myRule{}
    set size limit\ldots{} \myRule{}
  \end{tabular}}%
}
\newcommand{\mmaSkel}[1]{%
  \myBox[
    size=fbox,
    frame empty,
    left=1mm,
    right=1mm,
    arc=2.5mm,
    colback=black!30,
    coltext=white,
  ]{\(\cdots\) #1 \(\cdots\)}%
}

% Similar command probably should be available in package.
\newcommand{\mmaSummary}[4]{%
  \myBox[
    left=2mm,
    right=0mm,
    top=2mm,
    bottom=2mm,
    colback=black!3,
  ]{%
    \myBox[
      size=minimal,
      left=.3mm,
      right=.3mm,
      top=.5mm,
      bottom=0mm,
      colback=black!30,
      coltext=black!3,
    ]{%
      \textbf{\large +}}%
      \footnotesize%
      \begin{tabular}{l}
        \textcolor{black!50}{#1}#2 \\
        \textcolor{black!50}{#3}#4
      \end{tabular}%
  }%
}

\mmaSet{
  morelst={alsoletter={`}}, % This should be done in package itself.
%   morelst={morefvcmdparams=\mmaSummary 3,morefvcmdparams=\mmaTrunc 1},
  morefv={gobble=2},
  moredefined={Image,ImageData,img,na},
}

\begin{document}

% `\mmaGraphics{house}` works if there is `house...`
% file usable by `\includegraphics` command.
\begin{mmaCell}[index=3,moregraphics={moreig={scale=.2},yoffset=.7ex}]{Input}
  img = \mmaGraphics{house};
\end{mmaCell}

\begin{mmaCell}{Input}
  ImageData[img]
\end{mmaCell}

\begin{mmaCell}{Output}
  \mmaTrunc{\big\{\big\{\{0.615686, 0.568627, 0.870588,\\
  \mmaSkel{54}, \{0.619608, 0.576471, 0.882353\}\big\}, \mmaSkel{55}\big\}}
\end{mmaCell}

\begin{mmaCell}{Input}
  na = Image`InternalImageData[img]
\end{mmaCell}

\begin{mmaCell}{Output}
  NumericArray[\mmaSummary{Type: }{UnsignedInteger8}{Dimensions: }{\{56, 56, 3\}}]
\end{mmaCell}

\begin{mmaCell}[label=]{Input}
  Image[neg[na], "Byte"]
\end{mmaCell}

% `\mmaGraphics{house_neg}` works if there is `house_neg...`
% file usable by `\includegraphics` command.
\begin{mmaCell}[
  label=\textit{\color{black!60}Out[\textcolor{black!10}{\textbullet}]=},
  moregraphics={moreig={scale=.2},yoffset=.7ex}
]{Output}
  \mmaGraphics{house_neg}
\end{mmaCell}

\end{document}

Above code compiles to following PDF: PDF output

jkuczm commented 4 years ago

Fact that morelst={alsoletter={`}} had to be used in \mmaSet is a bug, this should be done in package itself.

jkuczm commented 4 years ago

Let's mark this issue as feature request to add support for truncated output and summary boxes.

abduld commented 4 years ago

Thanks a lot