I currently have lots of nice Mathematica codes in my main book project. Everything works fine. But I would like to have a nice feature : To be able to click on a code title, in the PDF output, to select the whole code, copy it and paste it directly into a Mathematica window, with all the formattings and colors to be respected. Is that possible ?
I suspect that the answer is "No", especially for the colors, but we never know...

Currently, some of the formattings are respected, but not all. And the colours are just forgotten, which is painfull to redo all by hand in Mathematica.
Here's a MWE to work with :
Code: Select all
\documentclass[12pt,letterpaper,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{listings}
\usepackage[colorlinks, linkcolor=black, urlcolor=red, citecolor=red]{hyperref}
\pagestyle{headings}
\usepackage{anysize}
\marginsize{1.5in}{1in}{0.5in}{0.5in}
\begin{document}
\lstset{tabsize=4,frame=single,language=mathematica,basicstyle=\scriptsize\ttfamily,keywordstyle=\color{black},backgroundcolor=\color{lightgray},commentstyle=\color{gray},
emph={Pi},emphstyle=\color{brown},
emph={[2]alpha},emphstyle={[2]\color{olive}},
emph={[3]},emphstyle={[3]\color{blue}},
emph={[4]},emphstyle={[4]\color{magenta}},
emph={[5]Ref},emphstyle={[5]\color{red}}}
\begin{lstlisting}[title={Mathematica code}]
(* This is a complicated line of code, for string theorists only : *)
alpha := 30Pi/180\end{lstlisting}
\begin{lstlisting}[title={Another Mathematica code}]
(* This is a very complicated line of code : *)
Pulsar := Graphics3D[{PointSize[0.02],Point[{0,0,0}]}]
\end{lstlisting}
\end{document}