0

I´m working on a song book. The TOC should consist of the Title of the piece and the respective composer.

This is how i do it now:

...
\newcommand{\titlecomponist}[2]{{#1}\hfill{#2}}
...
\begin{document}
\tableofcontents
\section[\titlecomponist{Hey Jude}{The Beatles}]{\songtitle{Hey Jude}}
%rest of the page...
\section[\titlecomponist{Smile}{David Gilmour}]{\songtitle{Smile}}

In the TOC I display both, and on the page itself only the songtitle. The problem is, "\hfill" sets the componist text to a set distance from the title, so the position is dependent from the length of the songtitle. But I want the componist on a fixed position on the page, like a table.

The only solution I found so far is to create said table manually, parse in my stuff and \newpage it. This would defeat the purpose of \tableofcontents however as it is not generating automatically. Also I can´t do that because I´m actually only writing a python script that translates easy input into LaTeX on file by file basis. It would rather complicate things implementing this solution, and I can´t accept the fact that there is no \hfill for fixed page positions or an even simpler two-column-solution for TOC.

Unfortunatly I have found only posts about deviding the whole TOC into two columns, putting entire text paragraphs on a fixed position and ChatGPT gives me the aforementioned overcomplicated table solution. To avoid any confusion, this is what I want it to look like in mcs Word: enter image description here

\documentclass{article}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{tgbonum}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{titletoc}

\definecolor{titleColor}{HTML}{EDAA25}
\definecolor{composerColor}{HTML}{1D594E}

\definecolor{chordColor}{HTML}{0A7373}
\definecolor{textColor}{HTML}{4D4D4D}
\definecolor{commentColor}{HTML}{F2C12E}

\newcommand{\songtitle}[1]{{\LARGE\bfseries\color{titleColor}#1}}
\newcommand{\composer}[1]{\hspace{1.35em}\small\color{composerColor}#1}

\newcommand{\ch}[1]{{\bfseries\color{chordColor}#1}}
\newcommand{\tx}[1]{\color{textColor}#1}
\newcommand{\com}[1]{\small\color{commentColor}#1}

\renewcommand{\contentsname}{Inhalt}
\titleformat{\section}{\normalfont\Large\bfseries}{}{2em}{}
\titlespacing{\section}{-10pt}{0pt}{0pt}


\newcommand{\titlecomponist}[2]{{#1}\hfill{#2}}

\titlecontents{section}[0em]
  {\addvspace{1em}}
  {\contentslabel[\thecontentslabel]{2em}}
  {\titelkomponist}
  {\hfill\contentspage}
  [\addvspace{0.5em}]

\geometry{
  left=2.5cm,
  right=3cm,
  top=2cm,
  bottom=2cm, 
}

\begin{document}
\tableofcontents
\newpage



%NEW SONG: C:\Users\cleme\OneDrive\code\liederbuch\input\Hey Jude.txt
\twocolumn

\section[\titlecomponist{Hey Jude}{The Beatles}]{\songtitle{Hey Jude}}

\composer{The Beatles}\\

%paragraph
\begin{tabular}{l}

\begin{tabular}{l@{}l@{ }l@{ }l}
\multicolumn{4}{l}{\com{Verse 1}}\\
\\[-0.5em]\ch{}&\ch{F}&\ch{G}&\ch{C}\\
\tx{}&\tx{Hey Jude, }&\tx{don't make it }&\tx{bad}
\end{tabular}\\

\begin{tabular}{l@{ }l@{ }l}
\\[-0.5em]\ch{}&\ch{C7}&\ch{F}\\
\tx{Take a }&\tx{sad song and make it }&\tx{better}
\end{tabular}\\

%about 300 more lines of this

\end{tabular}\\\\

\onecolumn
\newpage
%NEW SONG: C:\Users\cleme\OneDrive\code\liederbuch\input\Smile.txt
\twocolumn

\section[\titlecomponist{Smile}{David Gilmour}]{\songtitle{Smile}}

\composer{David Gilmour}\\

%paragraph
\begin{tabular}{l}

\begin{tabular}{l@{}l@{ }l@{ }l@{ }l}
\multicolumn{5}{l}{\com{Intro 2x}}\\
\\[-0.5em]\ch{}&\ch{C}&\ch{CM7}&\ch{Am}&\ch{CM7}\\

\end{tabular}\\

\end{tabular}\\\\

%paragraph
\begin{tabular}{l}

\begin{tabular}{l@{}l@{ }l}
\multicolumn{3}{l}{\com{Verse 1}}\\
\\[-0.5em]\ch{}&\ch{C}&\ch{CM7}\\
\tx{}&\tx{Would this }&\tx{do}
\end{tabular}\\

\begin{tabular}{l@{ }l@{}l}
\\[-0.5em]\ch{}&\ch{C}&\ch{CM7}\\
\tx{To }&\tx{make it all-}&\tx{right}
\end{tabular}\\

\begin{tabular}{l@{}l@{ }l}
\\[-0.5em]\ch{}&\ch{C}&\ch{D}\\
\tx{}&\tx{While sleep has}&\tx{ taken you}
\end{tabular}\\

\begin{tabular}{l@{ }l@{ }l}
\\[-0.5em]\ch{}&\ch{C}&\ch{CM7}\\
\tx{Where }&\tx{I´m out }&\tx{ of sight}
\end{tabular}\\

\end{tabular}\\\\

%another 200 lines of this

\onecolumn
\newpage
\end{document}
2
  • 1
    Welcome to TeX.SE! Please show us a short complete and compilable TeX code! Then we do not have to guess what you are doing ...
    – Mensch
    Aug 1, 2023 at 8:06
  • If there is a way to upload .tex files, i didn´t find it. So i removed a lot of the songtext because otherwise it has 500 lines of code. The content itself is devided by files, paragraphs and then lines. Files translate into pages, paragraphs into tables and lines into tables within the paragraph-tables. All of this is generated dynamically in python.
    – Lauch
    Aug 1, 2023 at 8:35

1 Answer 1

0

The first step is to put all the text into constant width boxes, with \hfill taking up any excess space. Note the use of \string to prevent the macros from being expanded too soon.

I found it easier to put everything into one command (\mysection) so that you only have to enter the arguments once.

The next step is to calculate the max width for titles and componists. Finally, these widths are copied to the aux file (see \AtEndDocument).

I'm not sure why a \shorttitle macro was needed, but it was.

\documentclass{article}

\newlength{\maxtitle}
\setlength{\maxtitle}{0pt}
\newlength{\maxcomponist}
\setlength{\maxcomponist}{0pt}

\edef\finaltitle{\the\dimexpr 0.35\textwidth}% will be replaced during \begin{document}
\edef\finalcomponist{\the\dimexpr 0.35\textwidth}

\newcommand{\mysection}[2]{% #1=title, #2=composer
\bgroup% measure widths
  \sbox0{#1}%
  \ifdim\wd0>\maxtitle\relax \global\maxtitle=\wd0 \fi
  \sbox1{#2}%
  \ifdim\wd1>\maxcomponist\relax \global\maxcomponist=\wd1 \fi
  \def\shorttitle{\string\makebox[\string\finaltitle][l]{#1}\string\hfill
    \string\makebox[\string\finalcomponist][l]{#2}\string\hfill}
  \section[\shorttitle]{#1}%
\egroup}

\makeatletter
\AtEndDocument{%
  \immediate\write\@auxout{\string\gdef\string\finaltitle{\the\maxtitle}}%
  \immediate\write\@auxout{\string\gdef\string\finalcomponist{\the\maxcomponist}}%
}
\makeatother

\begin{document}
%\finaltitle\par\finalcomponist

\tableofcontents
\mysection{Hey Jude}{The Beatles}

\mysection{Smile}{David Gilmore}

%\the\maxtitle\par\the\maxcomponist
\end{document}

You must log in to answer this question.

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