Text FormattingClickable Mathematica code ?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Clickable Mathematica code ?

Post by Cham »

I'm not sure this question belongs to this thread.

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}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Clickable Mathematica code ?

Post by Johannes_B »

No, i don t think this is possible. Mathematica has syntax highlighting? If so, you have to change the preferences in mathematica what the different control words shoul look like.

Allthough i have to say that i am not a fan of copying code. For a begginer it is often better to type in the stuff by hand, thinking while typing instead of blindly copying stuff.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Clickable Mathematica code ?

Post by Cham »

Copying the code by hand would be a nightmare ! The codes I'm showing are 2 pages long, on average !

I could select the code (or a part of it) in the PDF, copy it, and paste it into a Mathematica window. Some of the formatting is lost, but then this isn't a serious problem. But the color lost is a pain in the butt !

Mathematica doesn't do syntax highlighting. It is the user that select the command he want to colorize and apply a color on it, like in a RTF text.

So it's really not possible to retain the color, while copying a code in a PDF ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Clickable Mathematica code ?

Post by Johannes_B »

No. Color is a property that is defined in the current language. You can copy the glyphs, but not the attribute color. And even if you could, mathematica would need to understand that pdf attribute and properly hide it from the user.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Clickable Mathematica code ?

Post by Cham »

Hmmm,

I just made a simple test : Copying and pasting text from any RTF document retains the colors and styles. Copying text with colors from a PDF Mathematica code made with LaTeX could be pasted into a RTF document, without any trouble. It is retaining the colors. But Mma 7.0 don't use the RTF attributes for its input text. This is Mma's fault. Maybe more recent version fixes that ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: Clickable Mathematica code ?

Post by Stefan Kottwitz »

You could try a conversion instead of copy & paste or in addition, such as converting the PDF to RTF, keeping the colors, there are some tools.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: Clickable Mathematica code ?

Post by Cham »

The problem is Mathematica don't recognize the colors from outside of itself.

As stated above, I can copy the PDF code made with LaTeX, paste it in some RTF app, and all the colors are there. But if I copy this and paste into Mathematica, the colors are gone.
This is Mathematica's fault. So I don't think there's anything I could do on LaTeX side to solve my little problem.

Oh well, too bad !
Post Reply