How can I ensure nonbreaking spaces before every citation in pandoc markdown?

81 Views Asked by At

Let's take this example:

  • test.md:

    ---
    bibliography: test.bib
    ---
    
    This is just a test text to demonstrate that in some situations, unfortunately, citations will appear at the beginning of a line, which is often considered unaesthetic [@dominici2014overview].
    
  • test.bib:

    @article{dominici2014overview,
        title={An overview of Pandoc},
        author={Dominici, Massimiliano},
        journal={TUGboat},
        volume={35},
        number={1},
        pages={44--50},
        year={2014}
    }
    
  • acm.csl: http://www.zotero.org/styles/association-for-computing-machinery

Build:

pandoc test.md --citeproc --csl=acm.csl --bibliography=test.bib --pdf-engine=pdflatex -o test.pdf

This renders the PDF as:

This is just a test text to demonstrate that in some situations, unfortunately, cita-LINEBREAKtions will appear at the beginning of a line, which is often considered unaestheticLINEBREAK1.LINEBREAK1 Massimiliano Dominici. 2014. An overview of pandoc. TUGboat 35, 1LINEBREAK(2014), 44–50.

(Note that the [1] stands at its own line.)

How can I make sure that there is a nonbreaking space before the [1] so that it does not appear alone in the line? The obvious solution would be to use a \ instead of a simple in the Markdown, but this would clutter my Markdown file, so I'm looking for an automatic solution such as a configuration or a pandoc filter instead.

I even tried to write a simple filter, but apparently, the nonbreaking space is ignored, and this would require me to handle special cases such as the beginning of the sentence, so I would prefer an existing solution:

#!/usr/bin/env python

import pandocfilters as pf

def add_nonbreaking_space(key, value, format, meta):
    if key == 'Cite':
        value[1] = [{**text, 'c': f"\N{NO-BREAK SPACE}{text['c']}"} for text in value[1]]
        return pf.Cite(*value)
    return None

if __name__ == '__main__':
    pf.toJSONFilter(add_nonbreaking_space)
0

There are 0 best solutions below