LaTeX - score export

• Jan 20, 2021 - 08:18

Hi

I would like to write a music book with latex including text and music.

Latex -> for the style of the text and the ease of use
Musescore -> for the scores

However, currently, i am exporting the score as a PNG file and then cut it in photoshop as Musescore exports all the page and not only the score.

It results that I never have the same width and I loose lot of time to adjust the width of each samples.

What should I do ?

Cheers

Attachment Size
GAP.pdf 3.28 MB
008.mscz 9.18 KB
008-1.png 110.1 KB

Comments

In reply to by Jojo-Schmitz

Tₑχ/LᴬTᴇΧ don’t do SVG natively, but with a trick this is still the best way to go. You’ll need to have Inkscape installed.

Do this:

  • export score as SVG, let’s call it musica1.svg
  • if you have Inkscape 1.x (the current version), run inkscape -C --export-filename=musica1.pdf --export-type=pdf --export-pdf-version=1.4 musica1.svg
  • else you have Inkscape 0.x and run inkscape -C -z --export-pdf=musica1.pdf --export-pdf-version=1.4 musica1.svg
  • in your Tₑχ/LᴬTᴇΧ preample, add PNG support for graphics, that is, add the line \DeclareGraphicsExtensions{.pdf,.eps,.png,.jpg}% if you don’t have it yet, or add .pdf to the list if you already have it
  • then you can do the usual \includegraphics[width=\linewidth]{musica1}% dance

Enjoy! (And don’t hesitate to write back if you need assistance. I know Tₑχ/LᴬTᴇΧ are beasts, and I’ve also yet to tame them, although I got some common-to-me situations to work.)

MWE:


\documentclass{article}%
\usepackage{graphicx}%
\DeclareGraphicsExtensions{.pdf,.eps,.png,.jpg}%

\begin{document}
\includegraphics[width=\linewidth]{musica1}%
\end{document}

In reply to by mirabilos

If you need cropping, you can do it either before or in Tₑχ/LᴬTᴇΧ.

There’s either…


% trim: left down right up
\includegraphics[trim=0cm 25cm 0cm 3cm,clip,width=\linewidth]{musica1}%

… or…


% bb: lower left, upper right; from the lower left corner of the unscaled image
\includegraphics[bb=0cm 20cm 21cm 26cm,clip,width=\linewidth]{musica1}%

… but the units are a bit hard to figure out (neither of these examples worked correctly for me). bb normally takes bigpoints (1/72ᵗʰ inch), adding cm seems to work, but the input data is wrong because these are relative to the PostScript bounding box of the graphics file, which Inkscape sets from the SVG data.

Now MuseScore unfortunately exports with 141.732 pixels per centimetre (ca. 360 dpi) and Inkscape seems to convert based on the pixels…

In reply to by mirabilos

I wish you had stated your operating system. I seem to have found a really easy way to postprocess the MuseScore-exported SVG to make it load correctly by specifying the size not in pixels, but the conversion will need either hand-editing or some kind of automating tool, and I can provide a GNU/Linux (or BSD) solution now which may not help Windows® users…

In reply to by mirabilos

Right. If using the screenshot function you can also save as PDF, but if you have to go through SVG for some reason, you can convert the MuseScore-output SVGs to properly sized ones with the following command…

xmlstarlet ed -P -L -N svg=http://www.w3.org/2000/svg -u "/svg:svg/@*['px' = substring(., string-length(.) - 2 + 1)]" -x "concat(number(substring(., 1, string-length(.) - 2)) div 360, 'in')" musica1.svg

if xmlstarlet is installed. I’ve been informed there’s also an xmlstarlet for Windows, and this command probably also works on Windows. On Mac OSX, just do it in a Terminal.

The command is idempotent, it only converts _nnnn.nn_px to _nnn.nnnnnn_in in the attributes (width and height) of the top-level SVG tag and leaves the viewport alone (as must be).

In reply to by mirabilos

I don't have installed ImageMagick at time and it's a long time ago I needed it (probably at that time as third package for a desktop application ;-).
But if I remember correct it also supports the svg file format and you've some command line option to change the resolution with it (-resample, -density or maybe similar). But I never used it, I'm not familiar with it and I'm not sure if it would work (and if, if it would work with all file formats.)

In reply to by kuwitt

From Google:

Output SVG - ImageMagick
https://legacy.imagemagick.org › Board index › Users
1 Mar 2017 — ImageMagick is primarily a pixel processor. It can read many vector formats but destroys the vectors and replaces them with pixels. Although it ...

So, no, it does not support vector formats, except as input for rasterisation, and even then…

How to convert a SVG to a PNG with ImageMagick? - Stack ...
https://stackoverflow.com › questions › how-to-convert...
19 answers
25 Sep 2014 — I haven't been able to get good results from ImageMagick in this instance, but Inkscape does a nice job of scaling an SVG on Linux and ...

… it doesn’t do a good job at that.

In reply to by mirabilos

Also there's lots of tools in linux to manipulate PDFs. As an example these pop up from my commandline.

pdf180 pdf2svg pdfbook2 pdfetex pdfinfo pdfjam-slides6up pdfnup pdfsig pdftohtml pdftotext
pdf270 pdf90 pdfclose pdfflip pdfjam pdfjoin pdfopen pdftex pdftoppm pdfunite
pdf2dsc pdfatfi pdfcrop pdffonts pdfjam-pocketmod pdflatex pdfpun pdftk pdftops pdfxup
pdf2ps pdfbook pdfdetach pdfimages pdfjam-slides3up pdflatexpicscale pdfseparate pdftocairo pdftosrc

Also opening a PDF in gimp can be useful for starting with pretty documents in PDF, converting to what DPI you want, and then grabbing what you want or cropping what you don't want.

In reply to by Jojo-Schmitz

OK, maybe exporting directly as PDF from MuseScore is better, given the problems with its SVG export. You’ll have to specify the page in Tₑχ/LᴬTᴇΧ then.

  • export as e.g. musica2.pdf
  • add \DeclareGraphicsExtensions{.pdf,.eps,.png,.jpg}% like in the other answer
  • add page=_n_ when including the image
  • choose one of the two cropping types (trim takes away a specific amount, bb sets a bounding box)

MWE, using equivalent values for trim and bb so the output is the same for a DIN ISO A4 (21x29.7 cm) page (so you can do the maths):


\documentclass{article}%
\usepackage{graphicx}%
\DeclareGraphicsExtensions{.pdf,.eps,.png,.jpg}%

\begin{document}

with trim:

% trim: left down right up
\includegraphics[page=2,trim=0cm 20cm 0cm 3.7cm,clip,width=\linewidth]{musica2}%

\newpage

with bb:

% bb: lower left, upper right; from the lower left corner of the unscaled image
\includegraphics[page=2,bb=0cm 20cm 21cm 26cm,clip,width=\linewidth]{musica2}%
\end{document}

In reply to by jeetee

Best to save as PDF, which Tₑχ/LᴬTᴇΧ can use directly (see above).

There’s also a tool called pdfcrop, part of texlive-extra-utils (albeit without a manual page), which can automatically crop the background away from all PDF files, so you can that in combination with the \includegraphics[…]{name}% parameters to scale things to the correct size without any pixel artefacts.

Another option is to use ImageMagick to trim the white space from your images, that's what I have done in the past. We also implemented at one time a command line option for MuseScore to do this, but I'm not sure how well that works currently - I'm guessing it hasn't been tested in years.

Since I only need small bits of music at a time, I use "EasyABC" to make them and then the "abc package" in LaTeX to include them. Having both EasyABC and TexShop running at the same time, I can get the music the way I want it in EasyABC, save it as a "xxx.abc" file, and then use "\abcinput{xxx}" to bring it into my LaTeX document. If I wanted to have whole pages of music in my LaTeX document, I'd just make a PDF of them and then use the "pdfpages package" to put them in where I wanted them.

In reply to by abravov

That’s a great tool to have… iff the limitations are acceptable:

> This converter is primary used to transfer songs of Falešné společenstvo from Mozart music processor to MusixTeX typesetting notation via small subset of MusicXML 3.0 format.

It also has the same issue of the music losing the Mu͒seScore visual design. Again, that may or may not be okay, depending on the use cases.

Do you still have an unanswered question? Please log in first to post your question.