使用 Tikz 制作带有钢琴音符的大谱表

使用 Tikz 制作带有钢琴音符的大谱表

需要实现一个带有钢琴音符的大谱表。我尝试重用钢琴代码,但 tikzpicture 将钢琴图片绘制在乐谱之外。基本代码如下:

\documentclass[tikz,border=3mm]{standalone}
\usepackage[top=3cm, bottom=2.8cm, left=2.5cm, right=0.5cm, marginparwidth=0cm, marginparsep=0cm, headheight=13.6pt, a4paper]{geometry}
\usepackage[T1]{fontenc}
\usepackage{polyglossia}
\setmainlanguage{spanish}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc}
\tikzstyle{every picture}+=[remember picture]

\ExplSyntaxOn
\NewExpandableDocumentCommand \myFrequencyCalculation { m }
  { \fp_to_decimal:n { round(2**((#1-49)/12)*440,3) } }
\ExplSyntaxOff

\usepackage{musixtex} 

\begin{document}
Grand staff with piano notes:
\vspace*{2.5cm}

\begin{music}
    \parindent10mm
    %\sepbarrules
    \instrumentnumber{2}
    \setstaffs1{1}
    \setstaffs2{1}
    \setclef20
    \setclef16
    \smallmusicsize
    \curlybrackets{12}
    \nobarnumbers
    \font\A=phvb8t at 8pt% second fontsize, 4pt possible, too
    \startpiece%
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A A0}\wh{!`A}&\en
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A B0}\wh{!`B}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\bf\A C1}\wh{!`C}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\bf\A B1}\wh{!B}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\bf\A C2}\wh{!C}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\bf\A B2}\wh{!I}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\bf\A C3}\wh{!J}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\bf\A B3}\wh{!b}&\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\bf\A C4}\wh{!c}\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\bf\A B4}\wh{!i}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C5}\wh{!j}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B5}\wh{!p}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C6}\wh{!q}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B6}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C7}\wh{!x}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B7}\Ioctfinup 1{''s}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C8}\Toctfin1\wh{!x}\en
    \Endpiece
 \end{music}

 \pgfdeclarelayer{blacknotes}
 \pgfsetlayers{main,blacknotes}
 \tikzset{tight fit/.style={inner sep=0pt, outer sep=0pt}}
 
\begin{tikzpicture}[transform shape,scale=0.3]
\def\lastnotenodename{clefs}
\node[text width=1.0cm, tight fit] (clefs) at (0,0) {};
 
 \foreach \note [
     evaluate={
         \n=int(mod(\note-1, 12));
         \midi=int(\note+20);
         \octave=int((\note+8)/12);
         \t=int(floor((\note-1)/12)*7-7);
         \notename={"A","","B","C","","D","","E","F","","G",""}[\n];
         \tonicsolfa={"la","","si","do","","re","","mi","fa","","sol",""}[\n];
         \blacknote={0,1,0,0,1,0,1,0,0,1,0,1}[\n];
         \frequency={\myFrequencyCalculation{\note}};}
 ] in {1,...,88}{
 
     \ifnum\octave>3
         \tikzset{extract anchor/.style={anchor=south west, at=(\lastnotenodename.south east)}}
     \else
         \tikzset{extract anchor/.style={anchor=north west, at=(\lastnotenodename.north east)}}
     \fi
     \ifnum\blacknote=0
         \edef\notenodename{\notename_\octave}
         \node (\notenodename) [tight fit,text width=1cm, extract anchor/.try]  {};
         \xdef\lastnotenodename{\notenodename}       
         \node [anchor=base] (sol-fa)  at (\notenodename |- 0,-3) {%\tonicsolfa$_\octave$
         };
 
         \draw (\notenodename.south west |- 0,-4) rectangle ++(1, -4);
         \node [rotate=90, font=\footnotesize, anchor=east] 
             at (\notenodename.north |- 0,-4) {%\frequency
             };
         \node [font=\footnotesize, anchor=south]  
             at (\notenodename.south |- 0,-8) {%\midi
             };
         \node [font=\footnotesize, anchor=south] 
             at (\notenodename.south |- 0,-8.5)  {%\notename$_\octave$
             };
     \else
         \begin{pgfonlayer}{blacknotes}
         \fill ([xshift=-0.25cm]\lastnotenodename.north east |- 0,-4) rectangle ++(0.5, -2.5);
         \node  [rotate=90, text=white, font=\footnotesize, anchor=east]
             at (\lastnotenodename.north east |- 0,-4) {%\frequency
             };
         \end{pgfonlayer}
     \fi
 }
\end{tikzpicture}
 

\end{document}

当前输出如下: 当前的

期望的输出如下: 期望

答案1

standalone您的文档使用选项指定文档类tikz,这会导致每张图片都出现在新页面上。您看到的空间是您使用该geometry包请求的 A4 页面的其余部分。顾名思义standalone,该类适用于独立图片。

您似乎需要一份包含多项内容的文档。因此,请将其替换standalone为常规文档类,例如article

在此处输入图片描述

\documentclass{article}
\usepackage[top=3cm, bottom=2.8cm, left=2.5cm, right=0.5cm, marginparwidth=0cm, marginparsep=0cm, headheight=13.6pt, a4paper]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc}
\tikzstyle{every picture}+=[remember picture]

\ExplSyntaxOn
\NewExpandableDocumentCommand \myFrequencyCalculation { m }
  { \fp_to_decimal:n { round(2**((#1-49)/12)*440,3) } }
\ExplSyntaxOff

\usepackage{musixtex} 

\begin{document}
Grand staff with piano notes:
\begin{flushleft}
\begin{music}
    \parindent10mm
    %\sepbarrules
    \instrumentnumber{2}
    \setstaffs1{1}
    \setstaffs2{1}
    \setclef20
    \setclef16
    \smallmusicsize
    \curlybrackets{12}
    \nobarnumbers
    \font\A=phvb8t at 8pt% second fontsize, 4pt possible, too
    \startpiece%
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A A0}\wh{!`A}&\en
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A B0}\wh{!`B}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\bf\A C1}\wh{!`C}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\bf\A B1}\wh{!B}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\bf\A C2}\wh{!C}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\bf\A B2}\wh{!I}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\bf\A C3}\wh{!J}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\bf\A B3}\wh{!b}&\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\bf\A C4}\wh{!c}\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\bf\A B4}\wh{!i}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C5}\wh{!j}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B5}\wh{!p}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C6}\wh{!q}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B6}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C7}\wh{!x}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B7}\Ioctfinup 1{''s}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C8}\Toctfin1\wh{!x}\en
    \Endpiece
 \end{music}

 \pgfdeclarelayer{blacknotes}%
 \pgfsetlayers{main,blacknotes}%
 \tikzset{tight fit/.style={inner sep=0pt, outer sep=0pt}}%
 \begin{tikzpicture}[transform shape,scale=0.3]
\def\lastnotenodename{clefs}
\node[text width=1.0cm, tight fit] (clefs) at (0,0) {};
 
 \foreach \note [
     evaluate={
         \n=int(mod(\note-1, 12));
         \midi=int(\note+20);
         \octave=int((\note+8)/12);
         \t=int(floor((\note-1)/12)*7-7);
         \notename={"A","","B","C","","D","","E","F","","G",""}[\n];
         \tonicsolfa={"la","","si","do","","re","","mi","fa","","sol",""}[\n];
         \blacknote={0,1,0,0,1,0,1,0,0,1,0,1}[\n];
         \frequency={\myFrequencyCalculation{\note}};}
 ] in {1,...,88}{
 
     \ifnum\octave>3
         \tikzset{extract anchor/.style={anchor=south west, at=(\lastnotenodename.south east)}}
     \else
         \tikzset{extract anchor/.style={anchor=north west, at=(\lastnotenodename.north east)}}
     \fi
     \ifnum\blacknote=0
         \edef\notenodename{\notename_\octave}
         \node (\notenodename) [tight fit,text width=1cm, extract anchor/.try]  {};
         \xdef\lastnotenodename{\notenodename}       
         \node [anchor=base] (sol-fa)  at (\notenodename |- 0,-3) {%\tonicsolfa$_\octave$
         };
 
         \draw (\notenodename.south west |- 0,-4) rectangle ++(1, -4);
         \node [rotate=90, font=\footnotesize, anchor=east] 
             at (\notenodename.north |- 0,-4) {%\frequency
             };
         \node [font=\footnotesize, anchor=south]  
             at (\notenodename.south |- 0,-8) {%\midi
             };
         \node [font=\footnotesize, anchor=south] 
             at (\notenodename.south |- 0,-8.5)  {%\notename$_\octave$
             };
     \else
         \begin{pgfonlayer}{blacknotes}
         \fill ([xshift=-0.25cm]\lastnotenodename.north east |- 0,-4) rectangle ++(0.5, -2.5);
         \node  [rotate=90, text=white, font=\footnotesize, anchor=east]
             at (\lastnotenodename.north east |- 0,-4) {%\frequency
             };
         \end{pgfonlayer}
     \fi
 }
\end{tikzpicture}
\end{flushleft}
\end{document}

答案2

更新后的代码版本如下:

\documentclass[tikz,border=3mm]{article}
%\documentclass[tikz,border=3mm]{standalone}
\usepackage[top=3cm, bottom=2.8cm, left=2.5cm, right=0.5cm, marginparwidth=0cm, marginparsep=0cm, headheight=13.6pt, a4paper]{geometry}
\usepackage[T1]{fontenc}
\usepackage{polyglossia}
\setmainlanguage{spanish}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc}
\tikzstyle{every picture}+=[remember picture]

\ExplSyntaxOn
\NewExpandableDocumentCommand \myFrequencyCalculation { m }
  { \fp_to_decimal:n { round(2**((#1-49)/12)*440,3) } }
\ExplSyntaxOff

\usepackage{musixtex} 
\usepackage{xcolor}

\xdefinecolor{gray-undar}{RGB}{52,52,52}%0-0-100
\xdefinecolor{red-undar}{RGB}{179,35,79}%341-80-70 

\begin{document}
Grand staff with piano notes:
\vspace*{2.5cm}

\begin{music}
    \parindent10mm
    %\sepbarrules
    \instrumentnumber{2}
    \setstaffs1{1}
    \setstaffs2{1}
    \setclef20
    \setclef16
    \smallmusicsize
    \curlybrackets{12}
    \nobarnumbers
    \font\A=phvb8t at 8pt% second fontsize, 4pt possible, too
    \startpiece%
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A A0}\wh{!`A}&\en
    \notes\zcn{``B}{\hspace*{-0.10cm}\bf\A B0}\wh{!`B}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\color{red-undar}\bf\A C1}\wh{!`C}&\en
    \notes\zcn{``C}{\hspace*{-0.10cm}\bf\A B1}\wh{!B}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\color{red-undar}\bf\A C2}\wh{!C}&\en
    \notes\zcn{``H}{\hspace*{-0.10cm}\bf\A B2}\wh{!I}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\color{red-undar}\bf\A C3}\wh{!J}&\en
    \notes\zcn{A}{\hspace*{-0.10cm}\bf\A B3}\wh{!b}&\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\color{red-undar}\bf\A C4}\wh{!c}\en
    \notes&\zcn{`c}{\hspace*{-0.10cm}\bf\A B4}\wh{!i}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\color{red-undar}\bf\A C5}\wh{!j}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B5}\wh{!p}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\color{red-undar}\bf\A C6}\wh{!q}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B6}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\color{red-undar}\bf\A C7}\wh{!x}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B7}\addspace{-0.5em}\def\octnumberup{\ppffsixteen8$^{va}$}\octfinup{21}{1.3}\addspace{0.5em}\wh{!w}\en
    \notes&\zcn{`f}{\hspace*{-0.10cm}\color{red-undar}\bf\A C8}\wh{!x}\en
    %\notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A B7}\Ioctfinup 1{''s}\wh{!w}\en
    %\notes&\zcn{`f}{\hspace*{-0.10cm}\bf\A C8}\Toctfin1\wh{!x}\en
    \Endpiece
 \end{music}

 \pgfdeclarelayer{blacknotes}
 \pgfsetlayers{main,blacknotes}
 \tikzset{tight fit/.style={inner sep=0pt, outer sep=0pt}}
 
\begin{tikzpicture}[transform shape,scale=0.33]
%\node[anchor=south west,inner sep=0] (image) at (0,0) {
\def\lastnotenodename{clefs}
\node[text width=1.0cm, tight fit] (clefs) at (0,0) {};

 \foreach \note [
     evaluate={
         \n=int(mod(\note-1, 12));
         \midi=int(\note+20);
         \octave=int((\note+8)/12);
         \t=int(floor((\note-1)/12)*7-7);
         \notename={"A","","B","C","","D","","E","F","","G",""}[\n];
         \tonicsolfa={"la","","si","do","","re","","mi","fa","","sol",""}[\n];
         \blacknote={0,1,0,0,1,0,1,0,0,1,0,1}[\n];
         \frequency={\myFrequencyCalculation{\note}};}
 ] in {1,...,88}{
 
     \ifnum\octave>3
         \tikzset{extract anchor/.style={anchor=south west, at=(\lastnotenodename.south east)}}
     \else
         \tikzset{extract anchor/.style={anchor=north west, at=(\lastnotenodename.north east)}}
     \fi
     \ifnum\blacknote=0
         \edef\notenodename{\notename_\octave}
         \node (\notenodename) [tight fit,text width=1cm, extract anchor/.try]  {};
         \xdef\lastnotenodename{\notenodename}       
         \node [anchor=base] (sol-fa)  at (\notenodename |- 0,-3) {%\tonicsolfa$_\octave$
         };
 
         \draw (\notenodename.south west |- 0,-4) rectangle ++(1, -4);
         \node [rotate=90, font=\footnotesize, anchor=east] 
             at (\notenodename.north |- 0,-4) {%\frequency
             };
         \node [font=\footnotesize, anchor=south]  
             at (\notenodename.south |- 0,-8) {%\midi
             };
         \node [font=\footnotesize, anchor=south] 
             at (\notenodename.south |- 0,-8.5)  {%\notename$_\octave$
             };
     \else
         \begin{pgfonlayer}{blacknotes}
         \fill ([xshift=-0.25cm]\lastnotenodename.north east |- 0,-4) rectangle ++(0.5, -2.5);
         \node  [rotate=90, text=white, font=\footnotesize, anchor=east]
             at (\lastnotenodename.north east |- 0,-4) {%\frequency
             };
         \end{pgfonlayer}
     \fi
 }
%};
%\begin{scope}[x={(image.south east)},y={(image.north west)}]
\begin{scope}%[x={(clefs.south east)+8.5cm},y={(clefs.north west)}]
%Código para grillas
%\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
%\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (53,-12);
%\foreach \x in {0,10,...,530} { \node [anchor=north] at (\x/10,0) {0.\x}; }
%\foreach \y in {0,-10,...,-120} { \node [anchor=east] at (0,\y/10) {0.\y}; }
%Dibujando lineas y texto
\node[] (c1) at (3.0,-10) {\LARGE\color{red-undar}\textbf{C1}};
\draw [-latex, draw = red-undar, line width=1pt] (c1) -- (3.0,-8.1);

\node[] (c2) at (10.0,-10) {\LARGE\color{red-undar}\textbf{C2}};
\draw [-latex, draw = red-undar, line width=1pt] (c2) -- (10.0,-8.1);

\node[] (c3) at (17.0,-10) {\LARGE\color{red-undar}\textbf{C3}};
\draw [-latex, draw = red-undar, line width=1pt] (c3) -- (17.0,-8.1);

\node[] (c4) at (24.0,-10) {\LARGE\color{red-undar}\textbf{C4}};
\draw [-latex, draw = red-undar, line width=1pt] (c4) -- (24.0,-8.1);

\node[] (c5) at (31.0,-10) {\LARGE\color{red-undar}\textbf{C5}};
\draw [-latex, draw = red-undar, line width=1pt] (c5) -- (31.0,-8.1);

\node[] (c6) at (38.0,-10) {\LARGE\color{red-undar}\textbf{C6}};
\draw [-latex, draw = red-undar, line width=1pt] (c6) -- (38.0,-8.1);

\node[] (c7) at (45.0,-10) {\LARGE\color{red-undar}\textbf{C7}};
\draw [-latex, draw = red-undar, line width=1pt] (c7) -- (45.0,-8.1);

\node[] (c8) at (52.0,-10) {\LARGE\color{red-undar}\textbf{C8}};
\draw [-latex, draw = red-undar, line width=1pt] (c8) -- (52.0,-8.1);
\end{scope}
\end{tikzpicture}
 

\end{document}

输出如下: 输出

相关内容