Latex Tables from Fortran: Skipping the copy and paste step.

Creating tables in Latex can be a chore, especially when we have to copy and paste a lot of numbers. But since Latex files are only text, there is no reason why we can’t write formatted tables directly from Fortran programs.

I have written two short (380 lines total, including comments) Fortran classes to write Latex tables and have included them on my “Free Fortran” page. I hope you find them useful.

The classes are designed to be quick and easy to use. As such, they do not have a lot of options. Options are easy to add, and if you do, I’d appreciate if you would send me updates to include here.

Here’s how you use the classes. (Details can be found in a manual that I wrote for them.) The simplest is if you just want to make a table of heterogeneous numbers, either double precision or integer, from an array. You simply create an instance of the class LatexTableMaker and call one of its write subroutines, passing the column headers, caption and label that you want the table to have. (See my previous post about type bound procedures if this is new to you.)

      TYPE(LatexTableMaker) :: TMaker
     REAL(KIND(1.0d0)) :: tableData(4,5) ! Stored as (rows,columns)

      CALL TMaker % writeDoublePrecisionTableAsLatex(tableData = tableData, &
                                                     headers   = [“first ”,”second”,…,”last  ”],   &
                                                     caption   = “Prize Winning Results",   &
                                                     label     = “tab:PWR",     &
                                                     fileUnit  = 6)

Options include the format for the entries of the table, and whether or not the table has lines. There is an equivalent procedure for writing integer data.

For more flexibility, you can create tables of heterogeneous data where each column can have a different data type. Columns are defined by an instance of the LatexTableColumn class. Each column has a header, a Fortran format string, and a singly dimensioned array containing the data. Currently that would be INTEGER, DOUBLE PRECISION, or CHARACTER(256). Note the throwback to fixed length strings until gfortran catches up with the new standards. 

You fill a table column like this:

      CALL newLatexTableColumn(                                 &
                               tableColumn = arrayOfColumns(1),&
                               title        = "Names”,&
                               columnData   = [“Tom   ", “Dick ", “Harry"],&
                              columnFormat = "(A)") 

Once all the table columns are filled, you pass an array of them to the table maker:

      CALL TMaker % writeTableColumnArrayAsLatex(                              &
                                                 tableColumnsArray = arrayOfColumns,    &
                                                 caption           = caption, &
                                                 label             = label, &
                                                 fileUnit          = 6)

You can write tables out to any file, and then simply include the files at the appropriate places in any Latex document, thereby making the whole procedure automatic!

© Nocturnal Aviation Software 2011-2017. Mac and Mac OS are trademarks of Apple Inc., registered in the U.S. and other countries. iMac drawing by dinodigital. All other trademarks are the property of their respective owners. Privacy Policy. Press Releases.