Text FormattingTabbing within toc entries

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Tabbing within toc entries

Post by Pangur »

I have the following command in my preamble, so that I can easily typeset a hymn number and the corresponding text of its first line.
\NewDocumentCommand{\hymn}{mm}{\addsec{\textbf{#1}{\hspace{1em}}\textit{\small #2}}}

When I input a hymn, such as the following, \hymn{Hymn 574} {All things bright and beautiful} LaTeX produces the following result: "Hymn 574 All things bright and beautiful", which is fine.

By the time I am finished producing the booklet, there usually will be five hymns. For each hymn, I would like the text of its first line to be tabbed, so that the texts of all first lines would align vertically (say an inch in from the left). I have tried centring the first lines texts, and (as you can see [above]) have tried using a fixed space such as \hspace{1em}. However, if a hymn only has two digits in its number (e.g., Hymn 36) or only one digit (e.g., Hymn 3), the lines are, naturally, indented accordingly.

If I were writing a table in Excel or whatever, the numbers would be in one column, the words in the next column. Is it possible to mimic that using LaTeX? I am currenly using Koma-script.

As my wife has no need for the page numbers produced by the table of contents, I use
\renewcommand*{\contentsline}[3]{\csname l@#1\endcsname{#2}{}}
which works fine, except for the issue that I have mentioned already. However, I wondered if someone could show me if I could maybe manipulate that last piece of LaTeX to achieve what I want? Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Skillmon
Posts: 1
Joined: Fri Jan 18, 2019 9:49 pm

Tabbing within toc entries

Post by Skillmon »

Different approaches seem possible. Do you want the numbers to be right aligned, or just the space to the right of the numbers to be wide enough to make the line aligned? If the latter (and you want to keep the approach of putting the formatting code in the argument of \addsec -- restyling the ToC could be an option):

Code: Select all

\addsec{\rlap{\textbf{#1}}\hspace{5em}{\small\textit{#2}}}
Make sure the 5em is wide enough for the numbers/labels.

If the former and the "Hymn" does show up in every entry:

Code: Select all

\addsec{Hymn \makebox[<width-of-widest>][r]{#1}\hspace{1em}{\small\textit{#2}}}
If the former and the term "Hymn" isn't in every entry:

Code: Select all

\NewDocumentCommand \hymn { m m m } {\makebox[<width-of-widest-word>][l]{#1} \makebox[<width-of-widest-number>][r]{#2}\hspace{1em}{\small\textit{#3}}}
Those widths could be calculated programmatically.

All of the above could also be used by introducing proper changes to the ToC, instead of including formatting inside of an \addsec, especially with KOMA script, which has quite flexible ToCs.

It would be best if you add a complete MWE of your document (so everything from \documentclass to \end{document} that is needed to generate a small example output of what you've got so far.
Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Tabbing within toc entries

Post by Pangur »

Dear Skillmon,
Thank you so much for the way in which you have gone about answering my question. I reckon it is going to be very helpful to me.

I am vexed that I am not currently in a position to try out your helpful suggestions, as I am currently on holiday until 25 January. I hope it will be OK if I produce the MWE by 26 January.

I am so grateful for your advice, which is so well considered. I am sure it is going to be what I need.

Thanks very much.
Pangur
Posts: 29
Joined: Thu Jul 19, 2018 4:47 pm

Tabbing within toc entries

Post by Pangur »

I have now had a chance to try out the commands suggested by Skillmon, and they work entirely as expected. The variant that I have used is as follows.

\newcommand{\hymn}[3][MP]{\addsec{\rlap{\textbf{#1 #2}}\hspace{6em}{\small\textit{#3}}}}

This allows me to use hymns from Mission Praise [MP] by default, and if they should be from a different hymnal, I simply write in [SGP] for Songs of God's People, or [JP] for Junior Praise. As it is MP that is most often used, I only need write in the number for it, and the MP is put in front of the number by default. The spacing is precisely as I would wish, with the text of each hymn appearing exactly in vertical alignment with the other hymns.

Thanks to Skillmon for this suggestion, which exposed to me \rlap as possibility.
Post Reply