# 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

Maybe better use SVG?

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} 

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…

Looking further at this: Inkscape also has DPI conversion, and I got the wrong Inkscape command (fixed by editing) from another project. Investigating…

Update: see below, and perhaps better use PDF export.

Inkscape’s DPI conversion is just because 0.x read pixel values at 90 dpi while 1.x reads pixel values at 96 dpi.

Unfortunately, MuseScore exports pixel values at 360 dpi, so we still have a problem…

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…

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).

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.)

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...
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.

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} 

If you're always using the same page width and a full system, then look at using the image capture tool: https://musescore.org/en/handbook/image-capture

You can save your capture frame size, so it always uses the same size. But (as mentioned by Jojo) you can also use it to save in the SVG format, making your image pretty much lossless in quality.

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.