9

In technical documentation, sometimes the tool's automatic hyphenation makes a bad break in the middle of a term, like the name of an environment variable or function. In these cases I would rather have a short line than hyphenation, though I want hyphenation in the document in general. I can try to "write around" egregious cases to try to avoid the problem term being near the end of a line, but that's fragile. I'm looking for a solution that fixes all of them, without me having to individually handle each case.

I am using DocBook, which we transform to Formatting Objects (FO) and thence to PDF. Ideally I would like to be able to write a style directive that says "don't hyphenate inside these XML elements" and apply it to <classname>, <methodname>, and several others. This FO documentation describes a way to do this at the page-block level, e.g. to turn off hyphenation in a table of contents or a preface, but that's too coarse. This forum post suggests a way to hard-wire them within the text, meaning I would have to put a special directive around each class name, method name, and so on. (Also, it sounds like it didn't work for him.)

How can I most easily prevent bad hyphenation breaks in my code elements, working within the tool chain I have? (I'm not free to change that.)

4
  • Are you able to use CSS to style those elements? I have not tried this, but it seems as if hyphens:manual or hyphens:none would do thee trick. l.dhemery.com/1apHFhT Commented Oct 16, 2013 at 19:06
  • The end state here is PDF, so I don't think CSS is in play. But I could be wrong; thanks. Commented Oct 16, 2013 at 19:13
  • Is LaTeX an option? If so, it is pretty easy to do. See: tex.ac.uk/cgi-bin/texfaq2html?label=wdnohyph Commented Oct 17, 2013 at 4:09
  • No, our extensive documentation is in DocBook and porting to something else is not feasible. Besides, DocBook gives us important features that LaTeX probably lacks. No tool does everything; we chose the one that does most of what we need. Commented Oct 17, 2013 at 22:07

2 Answers 2

3

With help from a coworker I was able to fix this by adding the following to the FO stylesheet:

<xsl:template match="classname">
  <fo:inline hyphenate="false">
    <xsl:call-template name="inline.monoseq"/>
  </fo:inline>
</xsl:template>

And likewise for other elements that should get this treatment, like methodname and literal.

This creates a wrapper around the native style, changing hyphenation only.

Source

2
  • This seems like something that should be included in docbook-xsl FO. I can't think of a case where I would want to hyphenate classnames. Commented Jun 22, 2019 at 3:14
  • @RyanNowakowski yeah, you'd think. I don't know whether the current version does, but when I ran into this problem in 2013 it didn't. (I've since changed employer and thus tools.) Commented Jun 23, 2019 at 3:19
1

The post you referenced has the basics: in FO, you can't change the hyphenation property for just part of a block. You may be able to change the hyphenation dictionary (add the words you don't want to be hyphenated), but this depends on the tools you use. Information for FOP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.