使用 \makebox 在歌词上重叠和弦

使用 \makebox 在歌词上重叠和弦

我有以下代码可以在歌词上添加和弦:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{fontenc}
\usepackage{xcolor}
%Page Margins:
\usepackage[headheight=130pt,tmargin=130pt,headsep=50pt, a4paper, total={6in, 9in}]{geometry}

%Line Spacing, Paragraph indentation and Paragraph Spacing
\renewcommand{\baselinestretch}{1}
\setlength{\parindent}{0em}
\setlength{\parskip}{0.2em}

\newcommand\chord[2][l]{\makebox[0pt][#1]{\color{rgb,232:red,232;green,0;yellow,0}\begin{tabular}[b]{@{}l@{}}#2\\\mbox{}\end{tabular}}}

\begin{document}

This is a \chord{Am}li\chord{B}ne of text \chord{C}with chords\chord{Am}\chord{B}


\end{document}

生成(我使用pdflatex): 生成的线

问题很明显:当和弦太近时,它们会重叠。有没有办法让和弦自动向右移动,这样它们就不会重叠?我不介意和弦不在代码中放置它们的位置,只要它们清晰可见即可。欢迎提出任何建议!

答案1

与我的其他答案的输出相同,但这次\chord宏只接受一个参数。第二个参数是隐式的,由以下字符组成,最多四个。

This is a \chord{Am}li\chord{B}ne of text \chord{C}with chords\chord{Am}\chord{B}%

Text\chord{B} with a chord at the end of a word\chord{A}%

产量

在此处输入图片描述

如果您不喜欢填补上面和弦引入的空白的那行,请\hrulefill在命令的定义中删除\chordEnd

\documentclass{article}
\usepackage{xcolor}
\definecolor{chord}{RGB}{232,0,0}
% \chord{chord} + up to four characters as second arg
\newcommand\chord[1]{%
  \def\thischord{#1}%
  \def\lyrics{}%
  \chorda
}
\newcommand\chorda{\chordDo\chordb}% 1st character
\newcommand\chordb{\chordDo\chordc}% 2nd character
\newcommand\chordc{\chordDo\chordd}% 3rd character
% Continue this chain to include more characters
% Make sure to rename \chordd below to whatever was the last command above.
\newcommand\chordd{\chordDo\chordEnd}% last character
\newcommand\chordDo[1]{%
  \let\chordCont#1%
  \futurelet\chordFl\chordDoX
}
\newcommand\chordDoX{%
  \if\noexpand\chordFl\relax
    \let\tmp\chordEnd
  \else
    \ifcat\chordFl\space
      \let\tmp\chordSpace
    \else
      \let\tmp\chordChar
    \fi
  \fi
  \tmp
}
\newcommand\chordChar[1]{%
  \edef\lyrics{\lyrics#1}%
  \chordCont
}
\newcommand\chordSpace{%
  \edef\lyrics{\lyrics\ }%
  \afterassignment\chordCont
  \let\tmp=
}
\newcommand\chordEnd{%
  \begin{tabular}[b]{@{}l@{}}
    \textcolor{chord}{\thischord}\\
    \lyrics\hrulefill
  \end{tabular}%
}

\begin{document}
This is a \chord{Am}li\chord{B}ne of text \chord{C}with chords\chord{Am}\chord{B}%

Text\chord{B} with a chord at the end of a word\chord{A}%

\end{document}

答案2

你可以做这样的事情。

在此处输入图片描述

\documentclass{article}
\usepackage{xcolor}
\definecolor{chord}{RGB}{232,0,0}
% \chord{chord}{lyrics}
\newcommand\chord[2]{%
  \begin{tabular}[b]{@{}l@{}}
    \textcolor{chord}{#1}\\
    #2\hrulefill
  \end{tabular}%
}
\begin{document}

This is a \chord{Am}{li}\chord{B}{ne} of text \chord{C}{with chords}\chord{Am}{}\chord{B}{}

\end{document}

相关内容