[Tex/LaTex] How to easily include .ly files in a .tex document

lilypond

The context

I am generating an hymn booklet (containing mostly text), and I want to easily insert snippets of music scores based on .ly files I already have.

I have two kind of files:

  • my .tex files, that should be compilable without lilypond code in it
  • my .ly files, that I also use to generate .midi files and regular music sheets.

(Purpose is to be able to modify them independently – and to tidy up my main.tex.)

My question

I am looking for an easy way to include my already existing .ly files in my .tex document with such kind of pattern:

\documentclass{scrbook}
    [...]
\begin{document}

    Some regular text

    \begin{lilypond}
        \input{part/mylyfile.ly}
    \end{lilypond}
\end{document}

My trials

I have seen the principle of the two steps compilation with first lilypond-book, then (xe)latex ; an interesting for-newbee lilypond-in-latex-MWE ; and to finish, the automation of the compiling with arara

I am able to run the MWE and generate related .pdf document.

My issues

However, I'm stuck with following issues:

  • I have noticed that \input doesn't work with the lilypond-environment. Is there a way to call an external .ly file?

  • If I plain copy-paste my ly-file into the environment, I get plenty of errors, such as

     livret.lytex:40:44: Erreur : syntax error, unexpected '.', expecting SCM_FUNCTION or SCM_IDENTIFIER or SCM_TOKEN
      \override Lyrics.LyricText .font-shape = #'italic
    

    that refers to

    emph = {
      \override Lyrics.LyricText.font-shape = #'italic
    }
    
    • I also get errors for my \tuplet that I have to replace with \times.

Would you have any ideas of which idea I could explore in order not to be forced to re-write all my .ly code ?

Best Answer

As mentionned by touhami, the solution is to use the \lilypondfile[options]{myFile.ly} command. Available options are detailed in the lilypond online documentation.

Below is the workflow to compile your document:

The document

\documentclass{scrbook}
    [...]
\begin{document}

    Some regular text.

    \lilypondfile[noindent,quote]{myFile.ly}

\end{document}

Important: You have to save this file as filename.lytex and not as a .tex file.

The myFile.ly is a normal lylipond-file.

Compiling

(I give here my ready-to-go compiling work-flow. Of course, you need to adapt it to your needs.)

Working directory

In your working directory, you need:

  • Your filename.lytex file (see over),
  • The myFile.ly file you're calling in your .lytex file
  • A folder called LilyPondPrecompile

Commands

Via your terminal, go to your working directory. Then :

  1. Pre-compiling with lilypond-book (i.e. from .lytex to .tex)

    $ lilypond-book --pdf --format=latex --output=LilyPondPrecompile filename.lytex
    

    (Note that if you call your .ly file or other .tex files that are located in your workingDirectory/part sub-folder, you have to pass this argument into previous command line: --include=part)

  2. Move into your LilyPondPrecompile subfolder

    Previous step generated a .tex file out of the .lytex one. To keep folders clean, we decided to put the result (i.e. produced files) of this pre-compiling into a sub-folder (that what --output=LilyPondPrecompile stands for). We now have to change our working directory to compile the .tex file.

    $ cd LilyPondPrecompile/
    
  3. Compile the .tex file (i.e. from .tex to .pdf)

    $ pdflatex filename.tex
    

    Repeate this step if needed. (Note that calling pdflatex LilyPondPrecompile/filename.tex from your original working directory seems not to work).

Your done!

Related Question