\gtab [歌曲包] 使用宏变量作为第二个参数

\gtab [歌曲包] 使用宏变量作为第二个参数

\gtabs根据两个参数打印吉他和弦

\gtab{chord name}{guitar frets and strings}

第二个参数不接受用\def或定义的另一个宏变量\gdef。请参见以下示例以及图片:

\documentclass{article}
\usepackage{songs}

\newcommand{\hpvarB}[2]{
    \gtab{#1}{#2} 

}
\begin{document}
    The ``A'' chord prints as: \gtab{A}{002220}
    \\
    \def\hpvarA{A}{002220}
    However the chord graph does not print with macro variable as second parameter\par
    \gtab{A}{\hpvarA} 
    \\
    Only the chordname "A" is printed.  Any hints?\par 
    Macro hpvarB works!\hpvarB{B}{024442} But with hpvarA as \hpvarB{A}{\hpvarA} it does not

\end{document}

如果你没有安装该软件包,这可以为你提出的任何建议songs提供关于宏定义的提示\gtab

\newcommand\gtab{\SB@begincname\SB@gtab}
\newcommand*\SB@gtab[1]{%
    \SB@endcname%
    \begingroup%
    \catcode`:12\relax%
    \SB@@gtab{#1}%
}
\newcommand*\SB@@gtab[2]{%
    \endgroup%
    \ifnum\SB@transposefactor=\z@%
    \SB@@@gtab{#1}{#2}%
    \else%
    \begingroup%
    \let\gtab\SB@@@gtab%
    \gtabtrans{#1}{#2}%
    \endgroup%
    \fi%
}

在此处输入图片描述

答案1

\def\hpvarA{A}{002220}定义\hpvarA为具有扩展{A},并且文本002220在定义之后立即打印,因此这不是“粘合”在一起以形成正确的输入\gtab

要么定义\hpvarA为仅扩展为002220,这需要很多\expandafter语句,\hpvarB要么将其定义为

\def\hpvarA{{A}{002220}} (请注意里面的括号)和

\newcommand{\hpvarC}[1]{%
\expandafter\gtab#1%
}

或者说\expandafter\gtab\hpvarA或者只是\hpvarC{\hpvarA}

无论哪种情况,\hpvarA必须先扩展内容,然后才能将其输入到\gtab

\documentclass{article}
\usepackage{songs}

\newcommand{\hpvarB}[2]{%
  \expandafter\gtab\expandafter{\expandafter#1\expandafter}\expandafter{#2}% 
}

\newcommand\hpvarC[1]{%
  \expandafter\gtab#1%
}

\begin{document}
    The ``A'' chord prints as: \gtab{A}{002220}

    \def\hpvarA{{A}{002220}}
    \def\hpvarAother{002220}
    However the chord graph does not print with macro variable as second parameter\par
    \expandafter\gtab\hpvarA


    Only the chordname "A" is printed.  Any hints?\par 
    Macro hpvarB works!\hpvarB{B}{024442} But with hpvarA as \hpvarB{A}{\hpvarAother} it does not

    \hpvarC{\hpvarA}

\结束{文档}

在此处输入图片描述

答案2

  1. 正如其他人所提到的,宏\hpvarB包含将被标记为空间标记的空间,因此如果在水平模式或受限水平模式下处理,可能会导致不必要的水平空间。

  2. 该行执行以下操作:它定义要扩展为的\def\hpvarA{A}{002220}宏并打印嵌套在本地范围内的序列。 因此是:。您需要将的参数完全扩展为正确的值,然后才能让 LaTeX 执行。\hpvarAA002220
    \gtab{A}{\hpvarA}\gtab{A}{<something that expands to 'A'>}\gtab\gtab

您可以应用以下技术:

\documentclass[a4paper]{article}
\usepackage{songs}

\newcommand\PassFirstToSecond[2]{#2{#1}}%

\newcommand\OneLevelExpandSecondFirst[2]{%
  \romannumeral0\expandafter\PassFirstToSecond\expandafter{#2}{ #1}%
}%

\makeatletter
\newcommand\TotalExpandSecondFirst[2]{%
   \begingroup
   \protected@edef\mytempa{{#2}}%
   \expandafter\PassFirstToSecond\mytempa{\endgroup#1}%
}%
\makeatother

\newcommand\hpvarB[2]{\gtab{#1}{#2}}

\makeatletter
\newcommand\hpvarC[2]{\TotalExpandSecondFirst{\expandafter\gtab\@firstofone}{{#1}{#2}}}%
\makeatother

%-<Layout of example>----------------------------
\newcommand\horizmargins{2cm}%
\newcommand\vertmargins{2cm}%
\setlength\textwidth\paperwidth
\addtolength\textwidth{-\horizmargins}%
\addtolength\textwidth{-\horizmargins}%
\setlength\oddsidemargin{\horizmargins}%
\addtolength\oddsidemargin{-1in}%
\addtolength\oddsidemargin{-\hoffset}%
\setlength\evensidemargin{\oddsidemargin}%
\setlength\marginparwidth{\horizmargins}%
\addtolength\marginparwidth{-2\marginparsep}%
\setlength\columnseprule{\fboxrule}%
\setlength\headheight{0ex}%
\setlength\headsep{0ex}%
\setlength\footnotesep{\vertmargins}%
\setlength\footnotesep{.666\footnotesep}%
\setlength\textheight{\paperheight}
\addtolength\textheight{-\vertmargins}%
\addtolength\textheight{-\vertmargins}%
\setlength\topmargin{\vertmargins}%
\addtolength\topmargin{-1in}%
\addtolength\topmargin{-\voffset}%
\addtolength\topmargin{-\topskip}%
\addtolength\topmargin{\ht\strutbox}%
\pagestyle{empty}%
\setlength\parindent{0ex}%
\twocolumn
%------------------------------------------------

\begin{document}

\begin{verbatim}
\gtab{B}{024442}

\end{verbatim}
yields:\\
\gtab{B}{024442}

\hrulefill

\begin{verbatim}
\hpvarB{B}{024442}

\end{verbatim}
yields:\\
\hpvarB{B}{024442}

\hrulefill

\begin{verbatim}
\gtab{A}{002220}

\end{verbatim}
yields:\\
\gtab{A}{002220}

\hrulefill

\begin{verbatim}
\hpvarB{A}{002220}

\end{verbatim}
yields:\\
\hpvarB{A}{002220}

\hrulefill

\begin{verbatim}
\def\hpvarA{{A}{002220}}%
\expandafter\gtab\hpvarA
\end{verbatim}
yields:\\
\def\hpvarA{{A}{002220}}%
\expandafter\gtab\hpvarA

\hrulefill

\begin{verbatim}
\def\hpvarA{{A}{002220}}%
\expandafter\hpvarB\hpvarA
\end{verbatim}
yields:\\
\def\hpvarA{{A}{002220}}%
\expandafter\hpvarB\hpvarA

\newpage

\begin{verbatim}
\def\hpvarA{002220}%
\OneLevelExpandSecondFirst{\gtab{A}}{\hpvarA}
\end{verbatim}
yields:\\
\def\hpvarA{002220}%
\OneLevelExpandSecondFirst{\gtab{A}}{\hpvarA}

\hrulefill

\begin{verbatim}
\def\hpvarA{002220}%
\TotalExpandSecondFirst{\gtab{A}}{\hpvarA}
\end{verbatim}
yields:\\
\def\hpvarA{002220}%
\TotalExpandSecondFirst{\gtab{A}}{\hpvarA}

\hrulefill

\begin{verbatim}
\def\hpvarA{002220}%
\OneLevelExpandSecondFirst{\hpvarB{A}}{\hpvarA}
\end{verbatim}
yields:\\
\def\hpvarA{002220}%
\OneLevelExpandSecondFirst{\hpvarB{A}}{\hpvarA}

\hrulefill

\begin{verbatim}
\def\hpvarA{002220}%
\TotalExpandSecondFirst{\hpvarB{A}}{\hpvarA}
\end{verbatim}
yields:\\
\def\hpvarA{002220}%
\TotalExpandSecondFirst{\hpvarB{A}}{\hpvarA}

\hrulefill

\begin{verbatim}
\hpvarC{A}{002220}

\end{verbatim}
yields:\\
\hpvarC{A}{002220}

\hrulefill

\begin{verbatim}
\def\hpvarX{A}%
\def\hpvarA{002220}%
\hpvarC{\hpvarX}{\hpvarA}
\end{verbatim}
yields:\\
\def\hpvarX{A}%
\def\hpvarA{002220}%
\hpvarC{\hpvarX}{\hpvarA}

\end{document}

在此处输入图片描述

相关内容