friendly / VCDR

Visualizing categorical data with R
0 stars 0 forks source link

Code running off paragraphs (i.e., the \hbox is too wide problem) #36

Closed philchalmers closed 8 years ago

philchalmers commented 9 years ago

Seems one of the easiest ways to avoid this occurring globally is to modify how \code{} is defined by adding a \sloppy command beforehand. This will stretch the original line with the in-line code to meet the paragraph length, and move the unbreakable code portion to a new line.

Here's a minimal example:

\documentclass{article}

\newcommand{\code}[1]{\sloppy\texttt{#1}}

\begin{document}

text text text text text text text text text text text text text text  \texttt{somecode<-nottobebroken} text text text text text text text text text text text text text text text text text text text text text text text.

text text text text text text text text text text text text text text  \code{somecode<-nottobebroken} text text text text text text text text text text text text text text text text text text text text text text text.
\end{document}

There are other approaches that try to define what are called 'discretionary' rules to allow break at symbols like < when applicable, but I find these less aesthetically pleasing and don't always work well.

friendly commented 9 years ago

Hmmm, I tried modifying the definition of \code{} to

\makeatletter
\newcommand\code{\bgroup\@makeother\_\@makeother\~\@makeother\$\@codex}
\def\@codex#1{{\sloppy\normalfont\ttfamily\hyphenchar\font=-1 #1}\egroup}
\makeatother

but I don't see a decrease in \hbox too wide messages

philchalmers commented 9 years ago

Darn, this issue is tricky. I've tried a couple other things to fix this but with no luck. Hopefully the copy-editor will have some suggestions, otherwise we may have to manually put paragraphs in begin{sloppypar} .... \end{sloppypar} blocks....and even that might not work. sigh

friendly commented 9 years ago

This question has been asked and answered on tex stack exchange http://tex.stackexchange.com/questions/10377/texttt-overfull-hbox-problem

I'm not yet sure which solution to use.

philchalmers commented 9 years ago

I've tried basically all of these and they don't offer a global fix that is appropriate for this type of book. Setting discretionary points was one approach I tried, where code breaks at specific locations (like < or .), so

text text text thiscode<-thatcode

would show up as

text text text thiscode<- thatcode ....

But I'm not sure that's a good solution for R code, and it doesn't completely fix the problem either (thiscode can still run off the page!). The \sloppy command didn't seem to work well (though I couldn't test it in the book), and forcing breakpoints manually is both less than ideal and probably completely unwanted for code anyway, with and without a hyphen.

I haven't tried microtype with the book yet, but when I tried it with the article class it seems to really mess with the formatting, so I don't think it is applicable without a good amount of tweaking.

That leaves sloppypar, which I would try out, but I can't actually get the book to compile correctly anymore (various cryptic errors such as "! You can't use `\dimexpr' in restricted horizontal mode.", but the most interpretable error was that on line 2218 in book.aux the bibdata is pointing to graphics.bib, statistics.bib, etc...even when I delete the aux file, that line shows up again when regenerated and causes pdflatex to crash, so somehow those files are still being pointed to).

On Wed, Apr 15, 2015 at 9:33 PM, Michael Friendly notifications@github.com wrote:

This question has been asked and answered on tex stack exchange http://tex.stackexchange.com/questions/10377/texttt-overfull-hbox-problem

I'm not yet sure which solution to use.

— Reply to this email directly or view it on GitHub https://github.com/friendly/VCDR/issues/36#issuecomment-93613083.

philchalmers commented 9 years ago

Scratch that last part about book.aux; something must have been out of date. However, I still get this nasty message, resulting in a really distorted pdf file:

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'book.tex' failed.
LaTeX errors:
! You can't use `\dimexpr' in restricted horizontal mode.
<argument> \dimexpr 

l.3 \tableofcontents

! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@ 

l.3 \tableofcontents

! Missing number, treated as zero.
<to be read again> 
                   \def 
l.3 \tableofcontents

! Illegal unit of measure (pt inserted).
<to be read again> 
                   \def 
l.3 \tableofcontents

! Argument of \pgfmath@dimen@@ has an extra }.
<inserted text> 
                \par 
l.3 \tableofcontents

! Paragraph ended before \pgfmath@dimen@@ was complete.
<to be read again> 
                   \par 
l.3 \tableofcontents

! Extra }, or forgotten \endgroup.
<recently read> }

l.3 \tableofcontents

> 
friendly commented 9 years ago

If you haven't sorted this out already, pls fetch the latest version from github, get rid of non-essential book.* files and re-compile. The \dimexpr errors come from the code for the \chaptitle command defined in {book,chapter}.Rnw. Meanwhile, I discovered why my \code{} macro can't break -- it uses \ttfamily that doesn't allow breaks.
\texttt{}, \url{}, \path{} can be made to allow breaks, but the \code{} macro was also trying to avoid having to escape special characters that could occur in R code.

philchalmers commented 9 years ago

Commenting out the \chaptitle definitions with tikz (and just using #1 instead) seems to work well enough for me to get a workable copy, but I now get a few new messages that you might want to be aware of:

Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet,  : 
  Running 'texi2dvi' on 'book.tex' failed.
LaTeX errors:
! Undefined control sequence.
\@citeb ->\@nil 

l.26295 intervals for the category parameters.}

! Too many }'s.
l.8 \ix{fixed zeros}|see{structural zeros}}

You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.
! Too many }'s.
l.9 \ix{random zeros}|see{sampling zeros}}

You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.
! Too many }'s.
l.10 \ix{GLM}|see{generalized linear model}}

You've closed more groups than you opened.
Such booboos are generally harmless, so keep going.
! Too many }'s.
l.11 ...GLMM}|see{generalized linear mixed model}}

You've closed more groups than you opened.
Such booboos are 
friendly commented 9 years ago

These were problems in my inputs/indsee.tex file that I fixed. Sorry for the inconvenience.

philchalmers commented 9 years ago

Nesting problematic chunks in \begin{sloppypar} ... \end{sloppypar} seems to work fairly well here, and can be embedded within lists as well (tested it on some exercises in Chapter 2 with nice results). So this might be something to do as the book approaches the final copy.

philchalmers commented 8 years ago

I'll close this now that #56 is open and much cleaner.