\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{ebgaramond}
\usepackage[a4paper,margin=2cm]{geometry}
%% This two lines are needed only if you want to use \includeMTx.
\usepackage{mtxlatex}
\mtxlatex
\usepackage{hyperref}
\title{Including MusixTeX (M-TX) Scores on Overleaf}
\usepackage{pdfpages}
\usepackage{graphicx}
\newcommand*{\addFileDependency}[1]{
\IfFileExists{#1}{}{\typeout{No file #1.}}
}
\newcommand{\includeMTxPages}[1]{%
\addFileDependency{#1.pdf}%
\IfFileExists{#1.pdf}{\includepdf[pages=-]{#1.pdf}}{}
}
\newcommand{\includeMTxAsGraphics}[2][]{%
\addFileDependency{#2.pdf}%
\addFileDependency{#2-crop.pdf}%
\IfFileExists{#2-crop.pdf}{%
\bigskip\includegraphics[#1]{#2-crop.pdf}\bigskip%
}{}%
}
\newcommand*{\includeMTx}[1]{%
\IfFileExists{#1.tex}
{\input{#1}\typeout{Yeah we have #1.tex}%
{\addFileDependency{\jobname.mx2}}
}
{Oops sorry \typeout{No file #1.tex.}}
\addFileDependency{#1.tex}%
}
\begin{document}
%% Examples are taken from the MusicTeX documentation
Here's a snippet to be included as part of a page using \verb|\includeMTxAsGraphics| (as a PDF image):
\begin{center}
\includeMTxAsGraphics[width=.6\linewidth]{mozart}
\end{center}
Or you can try this alternate \verb|\includeMTx| command within a \texttt{mus} environment:
\begin{center}
\begin{mus}
\includeMTx{mozart}
\end{mus}
\end{center}
And the next piece will be inserted as a full page, with \verb|\includeMTxPages|.
When using \texttt{fontspec}, add this \emph{before} your \verb|\begin{document}|:
\verb|\PassOptionsToPackage{no-math}{fontspec}|
This is quite experimental so if you get an error, try \href{https://www.overleaf.com/learn/how-to/Clearing_the_cache}{clearing the cached generated files} in your compile session before recompiling.
If you have a lot of pieces and text to work on, it's probably a better idea to work on a music piece in a project on its own, then add the output to your main book project with \emph{`Add output from other project'}, to make debugging easier.
\includeMTxPages{netsook}
% We can't record/parse musixtex's log if there are errors though
\end{document}