Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a PDF with source code highlighting

335 views
Skip to first unread message

Beliavsky

unread,
Mar 10, 2022, 10:19:34 AM3/10/22
to
I would like to collect my Fortran tweets https://twitter.com/fortrantip into a PDF where the codes are in boxes with syntax highlighting. What software would you recommend for creating such a document?

I used LaTeX decades ago. It's powerful, but I have forgotten how to use it.

David Duffy

unread,
Mar 10, 2022, 4:08:37 PM3/10/22
to
I was going to suggest Lout, but that is even more of a niche approach
and you still have to learn the language ;). How about a literate
programming tool such as noweb, which can be used from the
WYSIWYG Lyx editor...

Beliavsky

unread,
Mar 10, 2022, 6:52:10 PM3/10/22
to
I can get Fortran syntax highlighting in HTML by pasting code at http://hilite.me/ , which does recognize Fortran among other languages. Presumably I could find a program that does this. Explanatory text can be added before the HTML block. Once I have an HTML file with text and code, there are ways to convert HTML to PDF.

Vincent MAGNIN

unread,
Mar 11, 2022, 2:25:20 AM3/11/22
to
Le 10/03/2022 à 16:19, Beliavsky a écrit :
> I would like to collect my Fortran tweets https://twitter.com/fortrantip into a PDF where the codes are in boxes with syntax highlighting. What software would you recommend for creating such a document?
>
> I used LaTeX decades ago. It's powerful, but I have forgotten how to use it.

Last year, I replaced the LaTeX lstlisting package by the minted package
in my documents:
http://tug.ctan.org/macros/latex/contrib/minted/minted.pdf

It is based on Pygments:
https://pygments.org/

Arjen Markus

unread,
Mar 11, 2022, 6:49:18 AM3/11/22
to
I use Latex quite a bit and the listlisting package is really nice. I do not know minted.

@beliavsky, we ought to be able to get up and running with Latex again ;). It is not at all difficult, unless you want to do sophisticated things.

Gary Scott

unread,
Mar 11, 2022, 9:47:36 AM3/11/22
to
On 3/10/2022 9:19 AM, Beliavsky wrote:
> I would like to collect my Fortran tweets https://twitter.com/fortrantip into a PDF where the codes are in boxes with syntax highlighting. What software would you recommend for creating such a document?
>
> I used LaTeX decades ago. It's powerful, but I have forgotten how to use it.
Sounds something ripe for MS Word mail merge via Excel perhaps.

Dan Nagle

unread,
Mar 11, 2022, 11:04:10 AM3/11/22
to
Hi,

On Mar 10, 2022, Beliavsky wrote
(in article<da13fa43-db95-40b6...@googlegroups.com>):
Michel Olagnon distributed, at one time, a "tops" program,
which produced a color-highlighted postscript listing from Fortran source.

I can't find a link now. :-(

I might be able to find a copy, contact me if all else fails. :-)

(If you have access to /glade, there might be a copy under ftools.)

--
Cheers!

Dan Nagle

John

unread,
Mar 12, 2022, 10:18:10 AM3/12/22
to
Do not know of a perfect solution out of the box, but know some places I would look.

I have used many utilities over time but some like f90ppr appear to be gone, or have not been kept up to date (enscript, ...) and you are posting modern syntax, so some that are up to date or nearly or still being updated that are non-commercial include lfortran, which can generate colorized output and emacs and vim, and the highlighters used primarily by extended markdown.

LFortran is under development and most likely to do submodules and more recent features properly, there are a lot of examples of how to extract the highlighted text from vim and emacs, I think the shell script by Steve Kinzler called colorize is a good example for getting HTML from vim without knowing vim, pandoc should be able to convert an extended markdown file but you might have to install the colorizers as separate packages, .... there are other pretty printers and colorizers that are parts of commercial compilers (so depends on what you have) and colorizers (mostly for supporting markdown variants) that are useable as seperate packages.

Did you find what you wanted?

Since the editors and web forums often support highlighters.

github and ford and doxygen are all using Fortran highlighters that might be seperate packages as well.

Did not expect the Olagnon site to disappear. There was quite a bit there still linked to by other sites like the Fortran Wiki

John

unread,
Mar 12, 2022, 11:59:15 AM3/12/22
to
One example using pandoc(1) on a linux box with bash(1). Make the Fortran file into an extended markdown file by adding ```fortran and ``` header and footer and use pandoc(1) to make and Adobe PDF file. read the PDF file into an HTML file using <IFRAME ...>. IFRAME gives you more control over geometry than <A....>. Could just do it as a markdown file and convert to HTML, etc. LOTS of options to play with with pandoc. Requires latex to be installed underneath for this particular pandoc command. Not a very robust script, but it shows the steps.

> #!/bin/bash
> set -x
> FFILE=${1:-/tmp/dice.f90}
> (
> echo '```fortran'
> cat $FFILE
> echo '```'
> ) >/tmp/show.md
> pandoc /tmp/show.md -s -o /tmp/fortran.pdf --highlight-style=kate -V geometry:margin=1in
> cat >/tmp/show.html <<\EOF
> <!DOCTYPE html>
> <html>
> <head>
> <title>Title of the document</title>
> </head>
> <body>
> This is some text to show
> <h1>PDF Example with iframe</h1>
> <iframe src="fortran.pdf" width="80%" height="500px">
> </iframe>
> </body>
> </html>
> EOF
> cd /tmp
> xdg-open show.html
> exit

John

unread,
Mar 12, 2022, 1:04:04 PM3/12/22
to
> lfortran fmt code.f90 |ansi2html >code.html

worked well with several modern Fortran files for coloring it; ansi2html is at

https://raw.githubusercontent.com/pixelb/scripts/master/scripts/ansi2html.sh

Did not see a way to write HTML directly from lfortran, although I thought there was one.

Beliavsky

unread,
Mar 12, 2022, 4:46:17 PM3/12/22
to
Thanks for this and your other suggestions. I emailed Michel Olagnon at Michel dot Olagnon at ifremer dot fr , but it bounced. It would be nice if someone who
has saved his tools can upload them. I may request this at Fortran Discourse in a week.

MAGNIN Vincent

unread,
Mar 13, 2022, 7:38:48 AM3/13/22
to
Here is a minimalist LaTeX document to start with. Under ubuntu, you
need the package python3-pygments. And you can compile the LaTeX file with:
$ pdflatex -shell-escape poly_algo.tex


\documentclass[12pt,english,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{xcolor}
\definecolor{bg_listing}{rgb}{0.95,0.95,0.95}

\usepackage{minted}
\setminted[Fortran]{linenos, autogobble, breaklines, breakanywhere,
bgcolor=bg_listing}
\setminted[Bash]{style=bw, frame=single, breaklines, breakanywhere}
\setmintedinline{bgcolor={}}

\begin{document}

\tableofcontents

\chapter{Introduction}

\chapter{Fortran Tip}

\section{Bitwise}

\begin{minted}{Fortran}
program bitwise
implicit none
print*,iand(1,1),iand(1,0),iand(0,1),iand(0,0) ! 1 0 0 0
print*,iand(2,2),iand(2,1),iand(2,0) ! 2 0 0
print*,ior(1,1),ior(1,0),ior(0,1),ior(0,0) ! 1 1 1 0
print*,ieor(1,1),ieor(1,0),ieor(0,1),ieor(0,0) ! 0 1 1 0
print*,iany([1,1]),iany([1,0]),iany([0,0]) ! 1 1 0
end program bitwise
\end{minted}

\end{document}

Arjen Markus

unread,
Mar 15, 2022, 9:53:46 AM3/15/22
to
I ran into difficulties using TeXnicCenter on WIndows (I was supposed to run Latex with some option). So I tried the example with the listings package. Here is the Tex code:

\documentclass[12pt,english,a4paper]{book}

%\usepackage[utf8]{inputenc}
%\usepackage[T1]{fontenc}
%\usepackage{lmodern}
%
%\usepackage{xcolor}
%\definecolor{bg_listing}{rgb}{0.95,0.95,0.95}
%
%\usepackage{minted}
%\setminted[Fortran]{linenos, autogobble, breaklines, breakanywhere,
%bgcolor=bg_listing}
%\setminted[Bash]{style=bw, frame=single, breaklines, breakanywhere}
%\setmintedinline{bgcolor={}}

\usepackage{xcolor}
\usepackage{listings}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{blue},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}

\lstset{style=mystyle}
\lstset{language=Fortran}


\begin{document}

\tableofcontents

\chapter{Introduction}

\chapter{Fortran Tip}

\section{Bitwise}

\begin{lstlisting}[language=Fortran]
program bitwise
implicit none
print*,iand(1,1),iand(1,0),iand(0,1),iand(0,0) ! 1 0 0 0
print*,iand(2,2),iand(2,1),iand(2,0) ! 2 0 0
print*,ior(1,1),ior(1,0),ior(0,1),ior(0,0) ! 1 1 1 0
print*,ieor(1,1),ieor(1,0),ieor(0,1),ieor(0,0) ! 0 1 1 0
print*,iany([1,1]),iany([1,0]),iany([0,0]) ! 1 1 0
end program bitwise
\end{lstlisting}

\end{document}

Just an alternative, mind you :).

Regards,

Arjen

0 new messages