2

I defined a new command for my novel method so that I can use it all over my document (and also easily change it later when I find a better one). I noticed that this results in lines running into the margins when my method name has a specific length. Here is a minimal example:

\documentclass[12pt]{article}
\usepackage[a4paper, left=2cm, top=2cm, bottom=2cm, right=4cm]{geometry}

\newcommand{\methodname}{MyMethod}

\begin{document} Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy \methodname~ eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \end{document}

enter image description here

As you can see, the first line first line is not rendered nicely. When I choose a longer name, at some point, the method name gets hyphenated but it still runs over the margin.

What is the source of the issue and what are the best ways to overcome it?

  • it was discussed below but I think the consensus is that this isn't always what one wants, for example when you have the method name at the end of the sentence – Milla Well Nov 09 '22 at 11:11

2 Answers2

5

~_ forces a double space, and prevents a line break, neither of which you want.

Also tex can't hyphenate MyMethod by default

enter image description here

\documentclass[12pt]{article}
\usepackage[a4paper, left=2cm, top=2cm, bottom=2cm, right=4cm]{geometry}

\newcommand{\methodname}{My-Method}

\begin{document} Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy \methodname\ eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \end{document}

David Carlisle
  • 757,742
  • Your first comment might be irrelevant, the problem persists with either ~_, _ or ~. no difference here. Your second comment does not seem to be correct. As I already said in the original question, when I choose a longer name (for example MyMethodmethod) it will it will introduce a hyphen after MyMethod. So it seems tex is able to hyphenate automatically but not at the correct position. – Milla Well Nov 08 '22 at 12:35
  • 1
    @MillaWell look at the output you show ~_ is a double space and prevents a line break. So tex can not hyphenate but you are also preventing it breaking at the space: it has to go as far as eir- before it can break. It is the main cause of your issue. I also don't know why you query my statement that tex can not hyphenate MyMethod. Add \showhyphens{MyMethod MyMethodmethod} You will see MyMethod MyMethod-method in the log showing tex finds no hyphenation points in the first and only one in the second – David Carlisle Nov 08 '22 at 12:42
  • With the additional explanation it all makes more sense. My main issue is actually the hyphenation not the ~_. Thanks! I didn't know the \showhyphens command. could you point me to where one can learn about the rules? – Milla Well Nov 08 '22 at 12:49
  • the ~ makes it much worse though as otherwise it would have broken the line after the method name. and ~_ is also wrong in all the cases where this ends mid-line so hyphenation is not involved at all, you do not want a double space. @MillaWell – David Carlisle Nov 08 '22 at 12:58
  • Just to be clear, you are suggesting to use \methodname\_ to allow for tex to have more options for line breaks in comparison to \methodname~? – Milla Well Nov 09 '22 at 11:12
  • 1
    @MillaWell yes (as long as by that you meant backslash space, not backslash underscore). Your original \methodname~ eirmod forces two spaces so is wrong even if no line break involved \methodname~eirmod is just one space but prevents a line break so would force the line overfull even if MyMethod fitted on the line \methodname\ eirmod puts a normal, breakable space after the name. – David Carlisle Nov 09 '22 at 11:45
2

As supplement to nice @David Carlisle answer (+1):

\documentclass[12pt]{article}
\usepackage[a4paper, 
            hmargin={2cm,=4cm}, vmargin=2cm]{geometry}
\hyphenation{My-Meth-od}            % <---
\newcommand{\methodname}{MyMethod}

\begin{document} Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy \methodname\ eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \end{document}

enter image description here

Zarko
  • 296,517
  • 2
    I agree with \hyphenation more general than my \- suggestion, but trailing space is dangerous, just hope you never need to end a sentence \methodname. – David Carlisle Nov 08 '22 at 12:28
  • @DavidCarlisle, you are right. I forgot on this possibilities. I corrected this. – Zarko Nov 08 '22 at 12:50
  • All possible use and styles of he \methodname command is unknown, but in presented case OP may risk with use xspace package and define new command as \newcommand{\methodname}{MyMethod\xspace} :-). But, better is stay on safe side and use it as suggested in your and mine answer. – Zarko Nov 08 '22 at 12:59
  • brilliant package, xspace I wonder who wrote it? :-) – David Carlisle Nov 08 '22 at 13:49
  • @DavidCarlisle, I know for the link and I know who are authors :-) – Zarko Nov 08 '22 at 14:05