1

I have written the following tex code

\documentclass[12pt]{book}
\usepackage[contents={}]{background} 
\usepackage{fontspec}
\setmainfont{Constantia}

\begin{document}
\LARGE
    
\chapter{Prayer}
True Gurus are very rare in this world, and of course, neither the true disciple! Only one who is full of Viveka (discrimination) and Vairagya (dispassion) is a qualified disciple. 

\end{document}

This produces the text as shown below : enter image description here

As we can see, this text is left aligned but somehow it is not getting right aligned (see the 3rd line) as well. How can i make it justified (both left & right aligned) ? Just to show the problem, i have written a small text here, but the problem is seen at many places. I see that by default the text in latex is justified, but not sure why it is not seen here. Can anyone please let me know why this issue is coming and how to resolve it. Thanks.

7
  • You can mark a possible hyphenation at the 3rd line, typing Vai\-raga.
    – Bernard
    Jul 11, 2021 at 12:14
  • 3
    I am unable to reproduce the issue you've encountered on an up-to-date TeX distribution (MacTeX2021, all updates applied), using either XeLaTeX or LuaLaTeX to compile your test document. Specifically, "Vairagya" is placed at the start of line 4 and the paragraph is fully justified. Which TeX distribution do you employ?
    – Mico
    Jul 11, 2021 at 13:12
  • 1
    As the first comments imply, your text may actually already be justified. However, since "Vairagya" is not an English word, LaTeX may simply not know were to hyphenate it and therefore it overflows into the margin. Do you have an overfull hbox warning in your log? If so, then adding the hyphenation as suggested above may solve your issue.
    – Markus G.
    Jul 11, 2021 at 14:29
  • @Mico & Markus : Thanks for the reply. I was seeing the issue with LuaLatex compiler. Now tried with XeLatex and i didn't see the issue in the line i pointed (vairagya comes properly on next line). But still it doesn't solve the issue fully. I am still seeing the issue coming up at several places, even with english words (2-3 alphabets seem to be going beyond the regular alignment). Also, i am seeing a lot of overfull hbox logs. Does the alignment work on best effort basis ? OR is there a way to get proper alignment for the entire document ? Also let me know, how to check the Tex distribution?
    – mezda
    Jul 11, 2021 at 15:19
  • 1
    @mezda - As I wrote earlier, I experience no problems of the variety you appear to have encountered when I compile your test document under LuaLaTeX. My system uses MacTeX2021, LuaHBTeX 1.13.2, and LaTeX2e <2021-06-01> patch level 1. You may find this information for your system by consulting the log file. (Hint: If you main tex file is called main.tex, then the log file's name will be main.log.)
    – Mico
    Jul 11, 2021 at 16:03

2 Answers 2

3

The issue will manifest itself when TeX is unable to break the line. It will emit an Ovefull hbox message and then manual intervention will be required to resolve the problem.

It can be solve by hyphenation, justified, ragged-right text, re-writing, an so on. It can happen with any font. In this example I used the default, Latin Modern Regular.

Another alternative to avoid the overfull lines is to use \emergencystretch to allow the inter word spaces to stretch.

I was introduced by Donald E. Knuth in TeX 3.0 together with a third pass for TeX’s line-breaking algorithm. (To discourage authors from using high values of the parameter \tolerance, which affects the entire document.)

The default value is 0pt, and can be augmented on a specific paragraph if the author (or editor) is unwilling or unable to change the text.

The best value to use and its aesthetic impact depends on the number of spaces between words in the column width and the number of characters to move to the next line.

From The optimal value for emergencystretch

Whatever value for the parameter \emergencystretch is used, the output should be checked to see if it does not create awful looking lines. The author of a text should think about a textual change before eliminating overfull lines with \emergencystretch.

Some experiments:

c

\documentclass[12pt]{article}
\usepackage{fontspec}

\begin{document}
    
    \hsize=379pt
    
    column width => \the\hsize
    
    emergencystretch default => \the\emergencystretch
    
    \LARGE
    True Gurus are very rare in this world, and of course, neither the true disciple! Only one who is full of Viveka (discrimination) and Vairagya (dispassion) is a qualified disciple. 
    
    \bigskip
    
    \normalsize \emergencystretch=7pt
    
    emergencystretch => \the\emergencystretch
    
    \LARGE
    True Gurus are very rare in this world, and of course, neither the true disciple! Only one who is full of Viveka (discrimination) and Vairagya (dispassion) is a qualified disciple. 
    
    \bigskip
    
    \normalsize \emergencystretch=0pt
    
    emergencystretch => \the\emergencystretch\, (Using hyphenation)
    
    \LARGE
    True Gurus are very rare in this world, and of course, neither the true disciple! Only one who is full of Viveka (discrimination) and Vai\-ragya (dispassion) is a qualified disciple. 
    
        \bigskip
    
    \normalsize \emergencystretch=0pt
    
    emergencystretch => \the\emergencystretch\, (re--written)
    
    \LARGE
    True Gurus are very rare in this world, and of course, neither the true disciple! Only one who is full of  Vairagya (dispassion) and Viveka (discrimination) is a qualified disciple. 
    
\end{document}
5
  • Thanks for the detailed answer. Just to make sure if i understood it correctly, is emergencystretch for setting the max tolerance spaces beyond the aligned text before a break occurs ? I am trying out with different values of emergencystretch and trying to optimize it to make sure not too many hyphens are seen at the line breaks. Also, can you please let me know if there is any option in tex which works similar to 'justify' option as available in MS word. Right now, i am manually trying to optimize the value for emergencystretch. Just wanted to check if there is any better opt.
    – mezda
    Jul 12, 2021 at 16:18
  • 1
    @mezda \emergencystretch was created to use paragraph by paragraph to eliminate overfull boxes, by allowing the stretching of spaces in that paragraph (need to restore back to 0pt after). You have to judge if the result is acceptable. \tolerance = integer number up to 10000 in the preamble will affect the whole document at once. Test my code with \tolerance = 500 and the first paragraph will be fixed and match the second. In a long document, you should go through the previous overfull boxes to see if the impact is acceptable, Justified paragraphs is LaTex default. Jul 12, 2021 at 18:09
  • 1
    @mezda There are several more efficient ways to control hyphenation. See tex.stackexchange.com/a/5039/161015 Using \usepackage{microtype} also helps to reduce their number. Jul 12, 2021 at 18:20
  • 1
    @mezda You can safely set \emergencystretch=\maxdimen (which is basically infinity)—the only downside will be some lines with very wide spaces, which is what MS Word does in such cases anyway. There are probably many answers on this site on what to do with overfull boxes in general; here is one of mine. Jul 12, 2021 at 18:47
  • Thanks all for the various suggestions. After trying different options, using the microtype package worked best for me. Apart from the proper alignment, it significantly reduced the number of hyphenations as well.
    – mezda
    Jul 13, 2021 at 14:23
0

Add the following to your document preamble and see if it solves your issue:

\usepackage{ragged2e}
\justifying
4
  • thanks for the suggestion, but even with this change the situation remains the same. Is there any other thing to try ?
    – mezda
    Jul 11, 2021 at 14:27
  • which TeX distribution you are using?
    – Tanvir
    Jul 11, 2021 at 14:28
  • how do i check that ?
    – mezda
    Jul 11, 2021 at 14:53
  • 2
    This reads more like a comment than an answer. BTW, the OP's issue is not that full justification isn't in effect. Instead, it is that the line break algorithm doesn't seem to be working correctly for some so-far-unknown reason and is producing at least one massively overfull line.
    – Mico
    Jul 11, 2021 at 16:05

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.