7

I have a situation where I've defined a custom environment. The environment always starts with "Property #" then some text describing the property. However, due to pdfLaTeX's automatic formatting, sometimes the number is stretched along with the text following the "Property #" heading (see MWE below).

\documentclass[letterpaper,12pt]{report}
\usepackage{amsmath}

\setlength{\parindent}{0pt}

% Creating a counter for the Property environment
\newcounter{ctrProperty}
% Defining a custom Property environment
\newenvironment{myProperty}{
   \refstepcounter{ctrProperty}
   \textbf{Property \thectrProperty}
   \itshape
}{\par}

\hyphenation{supercalifragilisticexpialidocious}

\begin{document}
\begin{myProperty}
A little bit of text.
\end{myProperty}

\begin{myProperty}
More text that causes a stretch because it ends in a word supercalifragilisticexpialidocious
\end{myProperty}
\end{document}

Example output

As you can see the 2 is slightly misaligned from the 1 in the preceding property, whereas I would prefer to have the same spacing between them and their corresponding "Property". It would also be nice if the 'A' and the 'M' were aligned, but maybe that's better as a separate question.

How can I prevent LaTeX from stretching the heading? Does a command along the lines of \nostretch{\textbf{Property \thectrProperty}} exist?

1
  • You could add \raggedright as part of the environment: \newenvironment{myProperty}{ \refstepcounter{ctrProperty} \textbf{Property \thectrProperty} \itshape\raggedright }{\par} May 19, 2017 at 15:47

4 Answers 4

5

Here, I keep the bolded property label from stretching by putting it in an \mbox, including the following blank space(s). Then, I invoke \ignorespaces after \itshape to squeeze out that extra liberty that LaTeX was taking in adding a space. I make sure lines of the environment definition end in % to fend off extra, unwanted, space tokens.

\documentclass[letterpaper,12pt]{report}
\usepackage{amsmath}

\setlength{\parindent}{0pt}

% Creating a counter for the Property environment
\newcounter{ctrProperty}
% Defining a custom Property environment
\newenvironment{myProperty}{%
   \refstepcounter{ctrProperty}%
   \mbox{\textbf{Property \thectrProperty} ~}%
   \itshape\ignorespaces%
}{\par}

\hyphenation{supercalifragilisticexpialidocious}

\begin{document}
\begin{myProperty}
A little bit of text.
\end{myProperty}

\begin{myProperty}
 \sloppy More text that causes a stretch because it ends in a word supercalifragilisticexpialidocious
\end{myProperty}
\end{document}

enter image description here

For a differnt approach, as I mentioned in a comment, one could alternatively typeset the text in \raggedright.

3

The simple space you insert gets interpreted by TeX as a normal space, ie it has a definite amount of stretchability and shrinkability (alongside its natural width). You could try and insert manually a hspace amount; I opted for 0.4em but you can choose which one looks the best for you ie:

\documentclass[letterpaper,12pt]{report}
\usepackage{amsmath}

\setlength{\parindent}{0pt}

% Creating a counter for the Property environment
\newcounter{ctrProperty}
% Defining a custom Property environment
\newenvironment{myProperty}{%
   \refstepcounter{ctrProperty}%
   \textbf{Property\hspace{.4em}\thectrProperty }\itshape%
}{\par}

\hyphenation{supercalifragilisticexpialidocious}

\begin{document}
\begin{myProperty}
A little bit of text.
\end{myProperty}

\begin{myProperty}
More text that causes a bit of a stretch because it ends in a word supercalifragilisticexpialidocious
\end{myProperty}
\end{document}

enter image description here

Thanks to @egreg for busting some spurious spaces

4
  • Spurious spaces alert!
    – egreg
    May 19, 2017 at 16:11
  • @egreg yup I noticed, I just forgot to post the correct version. Do I think is fine now…
    – Moriambar
    May 19, 2017 at 16:12
  • I count three of them. The space before \itshape should go before } instead, but you need to remove the three spurious spaces. Sorry, but Steven's answer is good and yours isn't.
    – egreg
    May 19, 2017 at 16:15
  • @egreg ok, thank you, I removed the ones I forgot and did as suggested. I agree that Steven's answer is better
    – Moriambar
    May 19, 2017 at 16:15
3

You might consider using enumitem and the enumerate environment for this, instead? Then you're guaranteed to have proper spacings for the labels. And the item body starts from the same line (the 'A' and 'M' as in your question).

\documentclass[letterpaper,12pt]{report}
\usepackage[showframe]{geometry} % not required in main file
\usepackage{enumitem}
\setlength{\parindent}{0pt}
\hyphenation{supercalifragilisticexpialidocious}

\newlist{myProperty}{enumerate}{1}
\setlist[myProperty]{before=\itshape,label=Property \arabic*, font=\normalfont\textbf, noitemsep, wide, resume}

\begin{document}
\begin{myProperty}
    \item A little bit of text.
    \item More text that no longer causes a stretch because it ends in a word supercalifragilisticexpialidocious
\end{myProperty}

Then some intermediate text.

\begin{myProperty}
    \item A little bit of text.
    \item More text that no longer causes a stretch because it ends in a word supercalifragilisticexpialidocious
\end{myProperty}

\end{document}

enter image description here

1

It seems more natural, semantically, to define your Property environment as a theorem environment, with a relevant theorem style, which is easy to define with thmtools. For the stretching problem, define hyphenation points for the long words LaTeX doesn't know how to hyphenate:

\documentclass[letterpaper,12pt]{report}
\usepackage{amsmath}

\setlength{\parindent}{0pt}
\usepackage{lipsum}
\usepackage{amsthm, thmtools}
\declaretheoremstyle[%
spaceabove=0pt,
spacebelow=0pt,
headfont=\bfseries,
bodyfont=\itshape,
headpunct=,
postheadspace=0.6em]{myplain}

\declaretheorem[style=myplain]{Property}

\hyphenation{super-cali-fragi-listic-expiali-docious}

\begin{document}

\lipsum[11]
\begin{Property}
  A little bit of text.
\end{Property}
\lipsum[11]
\begin{Property}
  More text that causes a stretch because it ends in a word supercalifragilisticexpialidocious
\end{Property}
\lipsum[11]

\end{document} 

enter image description here

You must log in to answer this question.

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