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.

Zarko
  • 296,517
Olöf
  • 73
  • 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 04 '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 04 '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 04 '16 at 22:33
  • This question may be relevant, if I understand your question properly: http://tex.stackexchange.com/questions/101545/different-vertical-spacing-parskip-between-concurrent-section-titles-and-parag – Steven B. Segletes Jun 05 '16 at 02:01
  • Hello everyone

    I use

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

    Because of the twocolumn, titles such as

    \section{This is a very very very long title and don't like it going over two lines*}

    Are oftentimes broken to over several lines. What I would like is, to have the characters squeezed, so that they fit in one line. I would like to have that automatically done. My guideline is: Shorten the title, if it's not readable anymore. But before, I'd liked it squeezed. And thank you everyone! This is really a great helping platform!

    – Olöf Jun 05 '16 at 09:30
  • 3
    A minimal working example is needed. (Bordaigorl and cfr already mentioned this) – cgnieder Jun 06 '16 at 10:35
  • I edited the question. See the picture above for an example how it should look like. – Olöf Jun 06 '16 at 13:40
  • 1
    Just out of idle curiosity: Why are you trying to do this? Why risk making a section header well-nigh unreadable? – Mico Jun 06 '16 at 20:14
  • Those aren't section titles and why are you so reluctant to provide an MWE which might actually enable somebody to provide effective help? I tend to assume you can't really be that interested in a solution. (That may be wrong, but it seems a reasonable working hypothesis.) – cfr Jun 06 '16 at 23:06
  • @Olöf: Are you after doing something like this? ...an extreme example, of course. If so, will you only use \section*, or perhaps non-starred \section as well? – Werner Jun 11 '16 at 12:16
  • @Werner Yes! That is what I was looking for. I only use \section. Does that make a difference, if I would use it starred or non-starred? The example text is really exaggerated. Normally my titles are so short that only one* word would go into the next line and that is really not nice :-) – Olöf Jun 11 '16 at 14:39

3 Answers3

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.

Werner
  • 603,163
  • 1
    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 09: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

rpapa
  • 12,350
Olöf
  • 73
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

Dai Bowen
  • 6,117
  • 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