1

After some comments, I am going to revise the question, so that it is more understandable.

Here is my MWE:

\documentclass[12pt, twocolumn, a4paper]{article}

\begin{document}
\section*{This is a very long title, that goes over several lines and I would like to have the characters squeezed, so that it fits in one single line}
\end{document}

This section title will go over several lines, since I use twocolumn for the document. I have two columns and thus less than half the witdth of an a4paper for the section title. Of course, this example is extreme and I do not have such long titles in my document. Mine do overflow the line by one word only or so.

Now, I am looking for code, that does automatically measure the length of the section title and then will adjust it (squeeze it!), so that the whole title finally fit's in one line.

Does anyone know how to do that? The characters shall be squeezed, as in condensed fonts, not the whitespaces reduced whithin the text.

I hope someone has a solution and thanks for all effort! Also, I hope the question is clear now.

  • 3
    Giving a proper answer that works for you requires knowing which classes and packages you are using. In other words, you should provide a MWE so we can help you – Bordaigorl Jun 4 '16 at 20:43
  • 1
    A generally useful pointer for this kind of needs is the titlesec package but if you are using special classes such as memoir or the KOMA classes then you have other specific options – Bordaigorl Jun 4 '16 at 20:44
  • 3
    Welcome! As mentioned, we need an MWE. Right now, I just find your question confusing anyway. Neither of the sample titles will exceed a line, so there is no need to squeeze them. (Unless your text block is 2mm wide or something.) Moreover, doing this automatically and in general seems to be a Very Bad Idea. – cfr Jun 4 '16 at 22:33
  • 3
    A minimal working example is needed. (Bordaigorl and cfr already mentioned this) – clemens Jun 6 '16 at 10:35
  • 1
    Just out of idle curiosity: Why are you trying to do this? Why risk making a section header well-nigh unreadable? – Mico Jun 6 '16 at 20:14
3

Here are some conditions that seem to be met:

  1. You only use \section*.

  2. You typically have section titles that are shorter than the column width, but occasionally roll over to two lines.

  3. Roll-overs should always fit on a single line.

(1) helps because it simplifies the measurement (there is no sectional number in the title). We can use xparse to redefine \section an manage the * gathering if you don't want to change your code.

For (3) we can use graphicx's \resizebox{<width>}{<height>}{<stuff>} to resize content to fit within the \linewidth.

enter image description here

\documentclass[twocolumn]{article}

\usepackage{xparse,graphicx}

\let\oldsection\section
\makeatletter
\RenewDocumentCommand{\section}{s o m}{%
  \sbox\z@{\normalfont\Large\bfseries #3}%
  \ifdim\wd\z@>\linewidth
    \oldsection*{\resizebox{\linewidth}{\ht\z@}{\usebox\z@}}
  \else
    \oldsection*{#3}
  \fi
}
\makeatother

\begin{document}

\section*{A short title}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\section*{This is a very long title, that goes over several lines and I would like to have the characters squeezed, so that it fits in one single line}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\section*{This is a very long title spanning two lines}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

\section*{XyzXXXXXXXXXXXXXXXX}
\section*{XyzXXXXXXXXXXXXXXXXXX}
\section*{XyzXXXXXXXXXXXXXXXXXXXX}
\section*{XyzXXXXXXXXXXXXXXXXXXXXX}
\section*{XyzXXXXXXXXXXXXXXXXXXXXXXX}
\section*{XyzXXXXXXXXXXXXXXXXXXXXXXXXX}

\end{document}

We store the \section title in a box and compare the width of that box against \linewidth. If it is wider, we shrink it horizontally to \linewidth, otherwise we just set the title as-is.

  • Isn't \resizebox{\linewidth}{!}{\usebox\z@} easier? – egreg Jun 11 '16 at 15:59
  • @egreg: That'll scale the height proportionally and for long-word-wrap-arounds might make the text seem squished vertically as well. But sure, it's an option. – Werner Jun 11 '16 at 16:01
  • @Werner Thank you! The solution works very well, just, what I was looking for. But another question: Could that may be extended towards: a) \subsection* b) \subsubsection* c) Titles of colorboxes where I use the following code: E.g. \begin{document} % \usepackage{tcolorbox} % \tcbuselibrary{skins,breakable} \newtcolorbox{ABox}[1]{colback=DarkGreen!45!white, colframe=LightSlateBlue!75!black, breakable, title={#1}} \end{document} – Olöf Jun 12 '16 at 9:57
0

I found a following solution:

\usepackage[tracking=true]{microtype}               
\newcommand\squeeze[1]{\SetTracking{encoding=*}{#1}\lsstyle}

I can call it e.g. by:

\section*{\squeeze{-75} *This is a very long title that is now being squeezed into one line*}

Nevertheless, I have to say "squeeze"! and by what amount ("-75") every time.

Does someone know a way, that a text is automatically squeezed into exactly one line?

Thank you everybody!

Olöf

0

The first comment has to be that shrinking titles beyond LaTeX's defaults is likely to be difficult to read or look out of place and so it's probably a better choice to rename the section.

Having said that, it might be preferable to have a \sectionshort[Short Title]{Full Title} command which prints <Short Title> if <Full Title> can't fit on the line. We could even prefer squeezing the full title before resorting to the short title. https://tex.stackexchange.com/a/19414 gives a nice explanation of how space between words are managed, to achieve the squeezing I've just reduced the normal spacing between words - there may well be alternative and preferable routes to do it.

\newlength{\reducedsectionspacing}
\setlength{\reducedsectionspacing}{0.333pt} %This changes the natural spacing between words
\newlength{\defaultsectionspacing}
\setlength{\defaultsectionspacing}{\fontdimen2\font}
\newlength{\sectiontitlelength}

\newcommand{\shortsection}[2][]{
\section{\setlength{\sectiontitlelength}{\widthof{\thesection\quad}+\widthof{#2}}\ifdim\sectiontitlelength<\linewidth#2
\else
\fontdimen2\font=\reducedsectionspacing
\setlength{\sectiontitlelength}{\widthof{\thesection\quad}+\widthof{#2}}\ifdim\sectiontitlelength<\linewidth#2
\else\fontdimen2\font=\defaultsectionspacing
\ifx\\#1\\#2\else#1
\fi\fi\fi
}}

First we test whether the given title is longer than the space available in the line, then we reduce LaTeX's default word spacing to \reducedsectionspacing and see if that's enough to put it on a single line. If that's not good enough we then put the short title if it's there otherwise the full title.

Using the following to test

\shortsection[This title is short]{This title is far far far far too long to be practical}
\shortsection[This title is short]{This title is far far far too long to be practical thus it gets ignored and replaced by the short version}
\shortsection[This title is short]{This isn't too long}
\setcounter{section}{0}
\shortsection{This title is far far far far too long to be practical}
\shortsection{This title is far far far too long to be practical thus it gets ignored and replaced by the short version}
\shortsection{This isn't too long}

We see that where feasible, titles have been squashed together, short titles substituted.

enter image description here

  • Dear Mr Bowen, Thank you for the idea. Unfortunately, it solves a substitution problem and not the original squeezing problem. But in other cases it is an interesting opportunity, thank you! – Olöf Jun 11 '16 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.