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?

  • Try using the url package and `\url{a/b/c/d/e} will allow linebreaking after a slash. – Peter Wilson Nov 6 '20 at 17:43
  • Unfortunately, that requires putting \url{} around every filename... – vy32 Nov 6 '20 at 21:56
  • That is the generic solution you asked for. – Peter Wilson Nov 7 '20 at 19:17
  • Thanks! @PeterWilson, I have edited the the question for clarity. – vy32 Nov 8 '20 at 20:06

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


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 \global\def 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.

  • perfect. Thanks. – vy32 Nov 9 '20 at 21:16

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.