6

I need to have the spaces in my code correspond to the spaces in the final pdf, but latex automaticly replaces a string of spaces with one space. Is it possible to somehow change this behaviour?

I need to use spaces as the unit and I need the code to not be longer than the actual space, and readable, so something like \hspace or \ won't work.

(I am trying to add chords to lyrics. I need them at the exact places, and I need to be able to see where they are going to end up, as I am writing them. I know, there are packages for this, but adapting my document for those would be way too much work (I am at some 8k lines right now))

example:

\documentclass{article}
\usepackage{tgcursor}
\renewcommand*\familydefault{\ttdefault}
\usepackage[T1]{fontenc}
\begin{document}
        A                 E
        
Should auld acquaintance be forgot
\end{document}

renders as

A E
Should auld acquaintance be forgot

I need it to render with the spaces, as they are in the code.

4
  • 2
    As always you are much more likely to get help if you provide code that others can copy and test as is. As your question is here, I doubt many people will understand (1) what you are doing and (2) what the actual problem is.
    – daleif
    Commented Sep 1, 2022 at 9:16
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Sep 1, 2022 at 9:40
  • 2
    Spaces don't have a constant width anyway, unless you're using a monospaced font. If you're using such a font, you could use verbatim mode to prevent LaTeX from processing spaces. Otherwise, using a tabular or some other structure providing tabulation is the way to go. As daleif said, you need to provide a MWE.
    – Miyase
    Commented Sep 1, 2022 at 9:44
  • I am using monospaced font for this reason, verbatim does exactly what I wanted to do. I consider this question answered.
    – aky-her
    Commented Sep 1, 2022 at 9:47

3 Answers 3

15

TeX ain't a word processor: it allows you to place the chords exactly where you want them, without any need to guess.

\documentclass{article}

\newcommand{\addchord}[2]{%
  \begin{tabular}[b]{@{}c@{}}\makebox[0pt]{#2}\\#1\end{tabular}%
}

\begin{document}

Should \addchord{au}{A}ld acquaintance b\addchord{e}{E} forgot

Should \addchord{au}{A}ld acquaintance b\addchord{e}{E} forgot

\end{document}

In the first line the chord is above the vowel (or diphthong), in the second case over the syllable, choose your style.

enter image description here

Do you want less vertical space? Just modify the definition:

\documentclass{article}

\newcommand{\addchord}[2]{%
  \begin{tabular}[b]{@{}c@{}}%
    \makebox[0pt]{\small #2}\\[-0.5ex]
    #1
  \end{tabular}%
}

\begin{document}

Should \addchord{au}{A}ld acquaintance b\addchord{e}{E} forgot

Should \addchord{auld}{A} acquaintance \addchord{be}{E} forgot

\end{document}

enter image description here

If you want minimal changes in the input files and are happy with monospaced font:

\documentclass{article}
\usepackage{tgcursor}
\usepackage[T1]{fontenc}
\usepackage{shortvrb}

\renewcommand*\familydefault{\ttdefault}
\MakeShortVerb{|}


\begin{document}

|       A                 E|

Should auld acquaintance be forgot

\end{document}

enter image description here

2
  • 1
    I know I can do things like this, but I already have a lot of work done (I was porting someone elses songbook) using the spaces and monospaced font, so verbatim is much better for me, eventhough generally speaking this is much better approach
    – aky-her
    Commented Sep 1, 2022 at 10:07
  • 1
    @aky-her I added a way to do verbatim with minimal markup.
    – egreg
    Commented Sep 1, 2022 at 10:16
6

You can also use one of the music packages at ctan, like package leadsheets. For demo-purposes I put some lines with and without chords.

demo song

\documentclass[12pt]{article}

\usepackage{leadsheets}

\begin{document}
  \begin{song}{title={Demo verses}, music={aky-her}}
    \begin{verse}
    
    Should \chord{A}auld acquaintance \chord{E}be forgot\\  
    and yada yada yada \dots{} \\
    and \chord{A}yada yada  \chord{E}yada \dots{} \\
  
    \end{verse}
  \end{song}
\end{document}
1
  • 1
    songs is another one I like; it makes heavy use of active characters, but that allows you to quickly and readably set songs with chords without too much TeX within the text. Commented Sep 2, 2022 at 9:00
2

My suggestion is to make a macro which would tell latex to typeset both a word and a chord in the same position, centred, with a risen chord by a vertical distance. This can be done with a mix of \clap{} and \hspace{}.

I also suggest to increase line spread e.g. to \doublespacing (from setspace).

enter image description here

\documentclass[12pt]{article}
\usepackage{setspace}

\NewDocumentCommand\chord{O{12pt}mm}{%
    % #1 vertical distance
    % #2 word
    % #3 chord
    \settowidth{\dimen0}{#2}%
    \hspace{\dimexpr\dimen0/2}%
    \raisebox{#1}{\clap{#3}}\clap{#2}%
    \hspace{\dimexpr\dimen0/2}}


\begin{document}
\parindent=0pt      % No indentation
\parskip=12pt       % vertical space between paragraphs
\setstretch{2}      % increased space between lines

Should \chord{auld}{A} acquaintance \chord{be}{E} forgot

Should \chord{auld}{A} acquaintance \chord{be}{E} forgot


\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .