Text FormattingDate range split to two lines in section title

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ckleiner
Posts: 7
Joined: Mon Jan 31, 2022 8:38 pm

Date range split to two lines in section title

Post by ckleiner »

I am trying to create a diary with a section for each day and a date on the same line on the right.
To save as much space as possible for the date, I use the \tiny font size.
Now in some cases, I even need to give a date range with a starting date and an ending date.
Such a range thereby becomes quite lengthy, thereby making the space available for the section quite small.
To minimise the required space for the date range, I would like to wrap the the date range to two lines, while keeping the alignment on the right.

Any help appreciated.

Code: Select all

\documentclass{book}
\begin{document}
  \section*{My new day with a quite lengthy title \hfill {\tiny Jan 1st, 2023 - Jan 10 2023}}
  The above date range should be split to two lines both on the right of the section title. The line break should be between the two dates (at the position of the - character)

  \section*{This is the next day \hfill {...}}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Date range split to two lines in section title

Post by rais »

Maybe something like this?

Code: Select all

\documentclass{book}
\newlength\drboxw
\newcommand*\rangedash{--}
\newcommand*\drbox[2][]{%
  \settowidth\drboxw{\tiny\rangedash#2}%
  \hfill\parbox[b]{\drboxw}{%
    \tiny\raggedleft
    \ifx\relax#1\relax\else %only if optional arg is non-empty:
      #1\par
      \rangedash
    \fi
    #2\par
  }%
}
\begin{document}
  \section*{My new day with a quite lengthy title \drbox[Jan 1st, 2023]{Jan 10, 2023}}
  The above date range should be split to two lines both on the right of the section title. The line break should be between the two dates (at the position of the - character)

  \section*{This is the next day \drbox{Jan 11, 2023}}

\end{document}
KR
Rainer
ckleiner
Posts: 7
Joined: Mon Jan 31, 2022 8:38 pm

Date range split to two lines in section title

Post by ckleiner »

Oh yes, thank you very much -> exactly what I needed!

Kind regards,
-- Christoph
Post Reply