TransWikia.com

Numbering the columns in a lstlisting environment

TeX - LaTeX Asked by jnrobinson on May 17, 2021

I’m trying to write some commentary about data that is formatted to a very old standard (ARINC 424) in which each record is a 132-column line and data fields are identified by the columns they occupy.

Thus, I would like to be able to number the columns in addition to the lines in a lstlisting environment, but I can’t find any way to do this in the listing package documents.

I was thinking that printing the column numbers along the top of the listing rotated 90 degrees CCW would be relatively readable. Does anyone have any idea how I could implement this?

Thanks in advance. I’m including a MWE but I don’t think it’s particularly useful since I don’t know how to approach this at all.

documentclass{article}

usepackage[utf8]{inputenc}
usepackage[letterpaper,margin=0.985in]{geometry} % weird margin so ARINC lines fit at 132 chars
usepackage{xcolor}
usepackage{listings} %for code snippets    
lstset{
    tabsize=4,
    numbers=left,
    numberstyle=ttfamilycolor{gray},
    frame = l,
    basicstyle=scriptsizettfamily,
    breaklines=true,
    postbreak=mbox{textcolor{red}{$hookrightarrow$}space},
}
usepackage[scaled=0.85]{beramono} %better typewriter font
usepackage[T1]{fontenc}

begin{document}    
    
begin{lstlisting}[
    float=hbt!, 
    caption=CIFP definition of the RNAV (GPS) Y RWY 13 approach procedure.,
    label=lst:kacyiap
    ]
SUSAP KACYK6FR13-Y ACARYL 010CARYLK6PC0EE AR   HF                     12790060    + 02000     18000                 A JS   090681310
SUSAP KACYK6FR13-Y AKOVEC 010KOVECK6PC0E  A    IF                                             18000                 A JS   090691310
SUSAP KACYK6FR13-Y AKOVEC 020CARYLK6PC0EE B 010TF                     03780060    + 02000                           A JS   090701310
SUSAP KACYK6FR13-Y AWAGIR 010WAGIRK6PC0E  A    IF                                             18000                 A JS   090711310
SUSAP KACYK6FR13-Y AWAGIR 020CARYLK6PC0EE B 010TF                     21790060    + 02000                           A JS   090721310
SUSAP KACYK6FR13-Y R      010CARYLK6PC0E  I    IF                                 + 02000     18000                 A JS   090731310
SUSAP KACYK6FR13-Y R      020MAYBNK6PC1E  F 051TF                     12790061    + 01600                           A JS   090741310
SUSAP KACYK6FR13-Y R      020MAYBNK6PC2WALPV       ALNAV/VNAV ALNAV                                                   JS   090751310
SUSAP KACYK6FR13-Y R      030RW13 K6PG0GY M 031TF                     12790046      00133             -300          A JS   090761010
SUSAP KACYK6FR13-Y R      040         0  M     CA                     1279        + 00275                           A JS   090771010
SUSAP KACYK6FR13-Y R      050JURADK6PC0E    010DF                                                                   A JS   090781310
SUSAP KACYK6FR13-Y R      060RODDIK6PC0EY   010TF                     0756        + 02000                           A JS   090791310
SUSAP KACYK6FR13-Y R      070RODDIK6PC0EE  R020HM                     21820050    + 02000                           A JS   090801310
end{lstlisting}
    
end{document}

One Answer

One way might be to write the column numbers above the first line in steps of 5.

This can be done as part of the lstlisting environment, but then it is difficult to customize - for example not putting a line number or frame, and print the column numbers in gray.

An alternative is to use two listings environments, one for the column numbers and one for the actual code. The floating environment makes this a bit tricky, but you can use the column numbers listing as float and the actual code as non-floating listing. This does not guarantee that the two listings stay on the same page so you need to check this carefully in the final document. The spacing above and below the two listings should also be adjusted using belowskip and aboveskip.

MWE:

documentclass{article}

usepackage[utf8]{inputenc}
usepackage[letterpaper,margin=0.985in]{geometry} % weird margin so ARINC lines fit at 132 chars
usepackage{xcolor}
usepackage{listings} %for code snippets    
lstset{
    tabsize=4,
    numbers=left,
    numberstyle=ttfamilycolor{gray},
    frame = l,
    basicstyle=scriptsizettfamily,
    breaklines=true,
    postbreak=mbox{textcolor{red}{$hookrightarrow$}space},
}
usepackage[scaled=0.85]{beramono} %better typewriter font
usepackage[T1]{fontenc}

begin{document}    

begin{lstlisting}[
    float=hbt!, 
    caption=CIFP definition of the RNAV (GPS) Y RWY 13 approach procedure.,
    label=lst:kacyiap,
    frame=none,
    numbers=none,
    basicstyle=scriptsizettfamilycolor{gray},
    belowskip=0pt,
    ]
    5    10   15   20   25   30   35   40   45   50   55   60   65   70   75   80   85   90   95   100  105  110  115  120  125  130
end{lstlisting}
begin{lstlisting}[aboveskip=-15pt]
SUSAP KACYK6FR13-Y ACARYL 010CARYLK6PC0EE AR   HF                     12790060    + 02000     18000                 A JS   090681310
SUSAP KACYK6FR13-Y AKOVEC 010KOVECK6PC0E  A    IF                                             18000                 A JS   090691310
SUSAP KACYK6FR13-Y AKOVEC 020CARYLK6PC0EE B 010TF                     03780060    + 02000                           A JS   090701310
SUSAP KACYK6FR13-Y AWAGIR 010WAGIRK6PC0E  A    IF                                             18000                 A JS   090711310
SUSAP KACYK6FR13-Y AWAGIR 020CARYLK6PC0EE B 010TF                     21790060    + 02000                           A JS   090721310
SUSAP KACYK6FR13-Y R      010CARYLK6PC0E  I    IF                                 + 02000     18000                 A JS   090731310
SUSAP KACYK6FR13-Y R      020MAYBNK6PC1E  F 051TF                     12790061    + 01600                           A JS   090741310
SUSAP KACYK6FR13-Y R      020MAYBNK6PC2WALPV       ALNAV/VNAV ALNAV                                                   JS   090751310
SUSAP KACYK6FR13-Y R      030RW13 K6PG0GY M 031TF                     12790046      00133             -300          A JS   090761010
SUSAP KACYK6FR13-Y R      040         0  M     CA                     1279        + 00275                           A JS   090771010
SUSAP KACYK6FR13-Y R      050JURADK6PC0E    010DF                                                                   A JS   090781310
SUSAP KACYK6FR13-Y R      060RODDIK6PC0EY   010TF                     0756        + 02000                           A JS   090791310
SUSAP KACYK6FR13-Y R      070RODDIK6PC0EE  R020HM                     21820050    + 02000                           A JS   090801310
end{lstlisting}
    
end{document}

Result:

enter image description here

Correct answer by Marijn on May 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP