TransWikia.com

Allow line-breaking after slashes in all cases without having to use a macro

TeX - LaTeX Asked on January 25, 2021

I am typsetting tables with longtable. I have a p{2in} column. I have filenames in the column. Sometimes these filenames are quite long. They have slashes in them. Sort of like /home/user/somebody/project1/file1/subfile1/this-is-the-filename.txt. Is there any way to tell LaTeX that it is allowed to line-break after a slash?

One Answer

Assuming that you don't have slashes in other contexts in your column, you could do something like this:

makeatletter
{catcode`/=active
  gdefslashbreak{
    catcode`/=active
    def/{char`/penaltyz@}}
}
makeatother
...
begin{slashbreak}
begin{longtable}
...
end{longtable}
end{slashbreak}

This will make / an active character inside the slashbreak environment so that any instance of it will allow a break afterwards.

Why it's done the way it is

Manipulating character codes can be tricky in TeX—the character code for a character will be the code when it’s digested, which is not necessarily what you might expect. If we were to leave out the surrounding braces and setting of the category code of /, we would get a missing control sequence when we tried to redefine / since the category code would be 12 rather than 13. This is also the reason why, for example, verb which does wholesale changes to category codes, doesn't work in the argument to another command: those changes happen too late for verbatim mode to work since the original category codes are established when TeX finally expands the verb macro.

The idiom that we employed here, of changing the category code inside a group and doing gdef (short for globaldef so that the definition is accessible outside the group) is typical for this sort of category code manipulation. If you dig through the LaTeX source code, you'll see a lot of similar coding happening.

Correct answer by Don Hosek on January 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP