1

tl;dr • Would lua be a good option to process metadata stored in an external text file and output TeX fragments for use in a front matter? A minimal lua example to solve the following sub-problem would be ideal:

Programming question

Given an external text file (full example below) containing a list with an arbitrary number (N) of authors and affiliations, how to programmatically produce the following text fragment?

\author[1,2,3]{Author One}
\author[2,*]{Author Two}

\affil[1]{First address}
\affil[2]{Second address}
\affil[3]{Third address}
\affil[*]{Corresponding author: email@my-email.com}

The program needs to perform the following tasks:

  • parse the input for authors and affiliations (optionally from an external file, not a critical feature here)
  • isolate the unique addresses from the complete list
  • produce the N \author[]{} directives and their corresponding unique \affil[]{}

This is a good example of a non-trivial task toward the more general goal described below; other macros would be more straight-forward (no need for for loops or conditional programming constructs). Note: we can assume that the macros \author and \affil are defined in a class, no need to worry about compiling the result. The question is about producing those fragments with lua.


Background • I've grown tired of the jungle of LaTeX macros defined by academic journal publishers. It's a huge waste of time – whenever one submits a manuscript they have to decipher the particular set of macros required for author names, affiliations, keywords, addresses, etc. with very little consistency across journals. Here's one example for illustration:

\title{Manuscript Title}
\dates{Compiled \today}

\author[1,2,3]{Author One}
\author[2,*]{Author Two}

\affil[1]{First address}
\affil[2]{Second address}
\affil[3]{Third address}

\affil[*]{Corresponding author: email@my-email.com}
\ociscodes{(140.3490) Lasers, distributed feedback; (060.2420) Fibers.}

and another:

\title{Manuscript title}
\date{\today}

\author{Author One}
\affiliation{First address}
\alsoaffiliation{Second address}
\email{email@my-email.com}

\author{Author Two} 
\phone{+123456789}
\fax{+123456789}
\email{email@my-email.com}
\affiliation{First address}
\alsoaffiliation{Second address}
\alsoaffiliation{Third address}

\abbreviations{IR,NMR,UV}
\keywords{American Chemical Society}

A substantial effort is often required to change the formatting for re-submission to another journal; I suspect it can sometimes influence the choice of a particular journal just to minimise this hurdle!

No-one's going to make publishers consider adopting a consistent set of macros – if anything, such an attempt would simply increment the number of existing "standards".

What I've been considering instead is developing a practical tool to work around this situation. Inspired by pandoc, the metadata required for a manuscript would be stored in an external, context-free and human readable format; the tool would process those data to populate a journal-specific template, resulting in LaTeX code that can be directly injected in the document.

As an illustration, consider this example:

_metadata.yaml

title: "Here and there again: harnessing non-locality in invisibility cloaks"
date: "\\today"

authors:
  - name: Bilbo Baggins
    affiliation: ["Bag End, Bagshot Row, Private Bag, Hobbiton, the Shire",
                  "Rivendell, West of the Misty Mountains, Eriador"]
    email: bilbo.baggins@hobbit.arda
    note: Currently on leave in Mordor
    corresponding: false
    collaboration: The Fellowship of the Ring

  - name: Lord Elrond
    affiliation: "Rivendell, West of the Misty Mountains, Eriador"
    email: elrond@noldor.arda
    corresponding: true

  - name: Lady Galadriel
    affiliation: ["Caras Galadhon, The Naith of Lórien, Lothlórien"]
    email: galadriel@noldor.arda
    corresponding: true

  - name: Gandalf The Grey
    affiliation: ["Middle-earth, Arda"]
    email: mithrandir@maiar.arda
    corresponding: true
    collaboration: The Fellowship of the Ring


thanks: Frodo Baggins, Gollum
keywords: [ring, invisibility, cloaking]
abbreviations: [LOTR]
pacs: [123, 456, 789] # https://publishing.aip.org/publishing/pacs
ociscodes: [123, 456, 789] # https://www.osapublishing.org/submit/ocis
preprint: APS/123-ABC

abstract: >
    All that is gold does not glitter. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This yaml metadata is read and processed by a script, and the resulting LaTeX macros inserted in the manuscript. The script contains various instructions to select and format the different parts according to a journal's specification. Switching between journals doesn't require changing the metadata file; only the set of string-processing instructions to output the journal-specific macros: depending on the choice author and address fields become,

\author{Author One}
\affiliation{First address}
\alsoaffiliation{Second address}
\email{email@my-email.com}

or

\author[1,2,*]{Author One}
\affil[1]{First address}
\affil[2]{Second address}
\affil[*]{Corresponding author: email@my-email.com}

and so on for other fields (some of them left unused). LaTeX then processes the manuscript as usual, with this header injected in the TeX file (\input{} or otherwise).

For instance, here is a minimal manuscript produced by the script, for use with a journal-specific class:

\documentclass[aip,graphicx]{revtex4-1}

\usepackage[]{lipsum}
\begin{document}

\title{Here and there again: harnessing non-locality in invisibility cloaks}

\author{Bilbo Baggins}    
\affiliation{Bag End, Bagshot Row, Private Bag, Hobbiton, the Shire}
\affiliation{Rivendell, West of the Misty Mountains, Eriador}
\author{Lord Elrond}
\affiliation{Rivendell, West of the Misty Mountains, Eriador}
\email{elrond@noldor.arda} 
\author{Lady Galadriel}
\affiliation{Caras Galadhon, The Naith of Lórien, Lothlórien}
\email{galadriel@noldor.arda} 
\author{Gandalf The Grey}
\affiliation{Middle-earth, Arda} 
\email{mithrandir@maiar.arda}
\date{\today} 

\pacs{123,456,789} 
\keywords{ring,invisibility,cloaking}

\begin{abstract}
All that is gold does not glitter. Lorem ipsum dolor sit amet.
\end{abstract}

\maketitle
\lipsum[2-6]
\end{document}

and another,

\documentclass[9pt,twocolumn,twoside]{optica}
\usepackage[]{lipsum}

\title{Here and there again: harnessing non-locality in invisibility cloaks}

\author[1,2]{Bilbo Baggins}
\author[2]{Lord Elrond}
\author[3]{Lady Galadriel}
\author[4]{Gandalf The Grey}

\affil[1]{Bag End, Bagshot Row, Private Bag, Hobbiton, the Shire}
\affil[2]{Rivendell, West of the Misty Mountains, Eriador}
\affil[3]{Caras Galadhon, The Naith of Lórien, Lothlórien}
\affil[4]{Middle-earth, Arda} 

\dates{\today}

\ociscodes{123, 456, 789}

\begin{abstract}
All that is gold does not glitter. Lorem ipsum dolor sit amet.
\end{abstract}

\begin{document}
\maketitle
\lipsum[2-6]
\end{document}

Some examples of output:

enter image description here

Note • I wrote a proof-of-principle in R because of familiarity with the language, but that's not a great choice here. I'm thinking of lua instead, since it's the language of choice for luatex – it's going to be available in most TeX distributions, and presumably has good facilities for this type of string-processing and interaction with TeX.

closed as off-topic by baptiste, Stefan Pinnow, Troy, Mico, Zarko Aug 24 '17 at 11:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – baptiste, Stefan Pinnow, Troy, Mico, Zarko
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Is the question final now? :-) If some Lua code takes data, identifies distinct addresses, and generates a unique \affil line for each, that answers your question fully, end of story? :-) Is there any question other than the programming question? (I notice “would Lua be a good…” but that's a one-word answer and you've already yourself explained it, so not sure what else you're asking.) – ShreevatsaR Aug 21 '17 at 2:24
  • Comments are not for extended discussion; this conversation has been moved to chat. – Joseph Wright Aug 24 '17 at 11:15
1

(Deleted some comments about the quality of the question.)

Here is an example that can get you started doing the same in Lua. It is a crude proof-of-concept, but you should be able to adapt it to your needs. (E.g. for simplicity it uses a simple key-value format for the metadata that happens to be actual valid Lua code, rather than parsing from a data-only format like YAML/JSON/XML/whatever. This is similar to what Emacs does with config files that are simply elisp code, what various Python projects like Django do where their config files are simply Python code, etc. But changing it to parse from YAML should be straightforward if you really need that.)

File gen.lua contains the templates (of course you'll want to move them to separate files) and a very basic template-substitution function:

journal1 = [[
\documentclass[9pt,twocolumn,twoside]{osajnl}
\journal{ol}
\setboolean{shortarticle}{true}

\title{$<title>}
\author[1,2,3]{$<author1>}
\author[2,*]{$<author2>}

\affil[1]{$<address11>, $<address12>, $<address13>}
\affil[2]{$<address21>, $<address22>, $<address23>}

\affil[*]{Corresponding author: $<email1>}

\dates{Compiled \today}
\ociscodes{(140.3490) Lasers, distributed feedback; (060.2420) Fibers}

\begin{abstract}
$<abstract>
\end{abstract}
\setboolean{displaycopyright}{true}

\begin{document}
]]

journal2 = [[
\documentclass[aip,graphicx]{revtex4-1}

\begin{document}

\title{$<title>}

\author{$<author1>}
\affiliation{$<address11>, $<address12>, $<address13>}
\email{$<email1>}
\author{$<author2>}
\affiliation{$<address21>, $<address22>, $<address23>}
\email{$<email2>}
\date{\today}

\pacs{123,456,789}
\keywords{$<keywords>}

\begin{abstract}
$<abstract>
\end{abstract}
]]

function substitute(template, variables)
   ret = template:gsub('($%b<>)', function(var) return variables[var:sub(3, -2)] end)
   tex.print(string.split(ret, '\n'))
end

And here is an actual .tex file that you could write, for a given case (you can move the data = into a separate file if you wish):

\directlua{dofile('gen.lua')}

\directlua{
data = {
  title = 'Here and there again: harnessing non-locality in invisibility cloaks',

  author1 = 'Bilbo Baggins',
  address11 = 'Bag End',
  address12 = 'Bagshot Row',
  address13 = 'Private Bag',
  email1 = 'bilbo.baggins@hobbit.arda',

  author2 = 'Lord Elrond',
  address21 = 'Rivendell',
  address22 = 'West of the Misty Mountains',
  address23 = 'Eriador',
  email2 = 'elrond@noldor.arda',

  abstract = 'All that is gold does not glitter. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',

  keywords = 'ring,invisibility,cloaking',
}
}

% Contains \documentclass and even \begin{document}
\directlua{substitute(journal1, data)}

\maketitle

Hello world
\end{document}

Processing the above file requires the files osajnl.cls and styles/ol.sty from the GitHub repository linked in the question. It produces:

journal1

and if you change journal1 to journal2 in the file (after deleting the .aux file), produces:

journal2

  • Thanks for the effort and sorry you feel this way. For what it's worth I have answered hundreds of programming questions on SO, and I don't think my question was particularly bad. I did not originally provide a minimal document because it wasn't necessary. The problem is about producing fragments (and those I provided, together with the metadata); they don't have to be run to be useful. On the other hand, hard-wired templates like your solution don't get me closer to a solution than the pandoc templates that I mentioned earlier, unfortunately. – baptiste Aug 20 '17 at 6:14
  • @baptiste I have answered hundreds of questions on multiple Q&A sites too. :-) I am happy to help newbies who don't know how to post a good question, but I draw the line at people like you who ought to know better. The problem, then and now, is that the question contains no clear criterion for what is an acceptable answer — having produced a fragment, how does one know it's what you want? (At minimum “It can be processed with tex” is necessary, which is why the classes were needed… if your question were clear about what output you want produced from what input, that wouldn't be needed.) – ShreevatsaR Aug 20 '17 at 6:25
  • @baptiste (re deleted comment) The only 3 things I can detect as questions are “Would lua be a good option…”, “A minimal example to get started would be ideal”, “What would be a good language…”. The answers to the first and last are trivial ("yes, sure", and "practically any language"), so I assumed the real question was the middle one (in Lua). If it's not, then I guess there is really no question here: is there really something you want advice on? What? (BTW I was going to add in the answer: for data I used literal key-value syntax for simplicity, but you can parse from yaml/json/etc too.) – ShreevatsaR Aug 20 '17 at 6:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.