Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reference a code block like the listings package? #36

Open
jkissl opened this issue Apr 26, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@jkissl
Copy link

commented Apr 26, 2017

Is there a way to reference the mathematica cell/code block like you would when using the regular listings package? Would an extension of this allow the creation of a list of code blocks like the \lstlistoflistings function does?

@jkuczm

This comment has been minimized.

Copy link
Owner

commented Apr 27, 2017

mmacells is using listings, but only through fancyvrb interface.

mmaCell is based on fancyvrbs Verbatim environment, so while we can pass options for listings using morelst key, we need some additional code to call listings captioning and labeling functions in Verbatim environment.

We can use morefv key to pass formatcom* option, with relevant code, to fancyvrb.

Putting it together:

\documentclass[12pt]{article}
\usepackage{mmacells}

\makeatletter
\mmaSet{morefv={formatcom*={%
  % Advance listings counter
  \refstepcounter{lstlisting}%
  % Use `\label` if `label` option was used.
  \ifx\lst@label\@empty\else
    \label{\lst@label}
  \fi
  % Add entry to list of listings if `caption` option was used.
  \ifx\lst@@caption\@empty\else
    \addcontentsline{lol}{lstlisting}{\protect\numberline{\thelstlisting}\lst@@caption}
  \fi
}}}
\makeatother

\begin{document}

Cell with entry in list of listings:
\begin{mmaCell}[morelst={caption=mma}]{Input}
a+b
\end{mmaCell}

Labeled cell:
\begin{mmaCell}[morelst={label=myCell}]{Input}
c+d
\end{mmaCell}

Link to \hyperref[myCell]{labeled cell}.

\lstlistoflistings

\end{document}

If you don't want to mix Mathematica cells with other code listings you'd need separate counter and separate table of contents.

@jkuczm

This comment has been minimized.

Copy link
Owner

commented Nov 13, 2017

If one wants to use automatically named references using \nameref command provided by hyperref bundle, following code will use caption as name of reference, or, if caption is not given, In[...] or Out[..] label.

\documentclass[12pt]{article}
\usepackage{mmacells}

\ExplSyntaxOn
\makeatletter

\newcounter { mmacells }

\clist_put_right:Nn \l_mmacells_fv_keyval_clist
  {
    formatcom* =
      {
        \refstepcounter { mmacells }
        \cs_if_eq:NNF \lst@label \@empty
          {
            \cs_if_eq:NNTF \lst@@caption \@empty
              {
                \bool_if:NT \l_mmacells_indexed_bool
                  {
                    \cs_set:Npx \@currentlabelname
                      {
                        \bool_if:NTF \l_mmacells_intype_bool { In } { Out }
                        [ \int_use:N \g_mmacells_index_int ]
                      }
                  }
              }
              { \cs_set:Npx \@currentlabelname { \lst@@caption } }
            \label { \lst@label }
          }
      }
  }

\makeatother
\ExplSyntaxOff

\begin{document}

\begin{mmaCell}[morelst={label=twoLineInput}]{Input}
2 + 2
a+x+a
\end{mmaCell}

\begin{mmaCell}[morelst={label=numericOutput}]{Output}
4
\end{mmaCell}

\begin{mmaCell}[morelst={label=symbolicOutput}]{Output}
2 a + x
\end{mmaCell}

\begin{mmaCell}[morelst={label=favCell,caption=my favorite cell}]{Input}
c
\end{mmaCell}

Input cell: \nameref{twoLineInput}.\\
Numeric output cell: \nameref{numericOutput}.\\
Symbolic output cell: \nameref{symbolicOutput}.\\
Last cell: \nameref{favCell}.

\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.