4

Here, I use the function \txtbrut to write in typewritter font certains things such as file names or functions in python, matlab, etc. I use listings for the colouring of the code and because it does a better job at cutting names than \texttt. But it is not at a 100% reliable, and I do not need here the same convention as for normal text.

How could I modify my function \txtbrut so it allows break anywhere, with no hyphenation ?

\documentclass{report}
\usepackage{fontspec}
\usepackage[hmargin=8cm,showframe]{geometry}

\usepackage{listings}
\newcommand*{\txtbrut}{\lstinline[basicstyle=\ttfamily]}%[

\begin{document}

\txtbrut{Iletaitunefoisunejeuneetjolieprincessequiaimaiteclaterlecranedesesennemis.txt}

\end{document}
HcN
  • 1,274
  • 9
  • 25

1 Answers1

2

Here, I compare the OP's \txtbrut to an alternate \alttxtbrut. The \alttxtbrut code was adapted from one of egreg's answer's as an alternative to the \url macro, as I recall (maybe someone could provide a link). Even so, one may still be required to place the paragraph in a sloppypar to get the margins just so.

Now if non-alphanumeric characters are needed (I am not sure of the syntax requirements of the languages he/she mentions), adjustments would be necessary.

\documentclass{report}
%\usepackage{fontspec}
\usepackage[hmargin=8cm,showframe]{geometry}

\usepackage{listings}
\newcommand*{\txtbrut}{\lstinline[basicstyle=\ttfamily]}%
\newcommand\alttxtbrut[1]{\tbhelp#1\relax\relax\relax}
\def\tbhelp#1#2\relax{%
  {\texttt{#1}}\penalty0\ifx\relax#2\else\tbhelp#2\relax\fi}
\begin{document}

\txtbrut{Iletaitunefoisunejeuneetjolieprincessequiaimaiteclaterlecranedesesennemis.txt}

\begin{sloppypar}% <--MAY BE NEEDED
 \alttxtbrut{Iletaitunefoisunejeuneetjolieprincessequiaimaiteclaterlecranedesesennemis.txt}
This is the text that follows.
\end{sloppypar}

\end{document}

enter image description here

  • Thanks for you reply. It works quite good, but indeed I use this as a verbatim environment to be able to insert special characters (well, mainly underscores). Is there a way to adapt your code to take these into account ? – HcN Mar 03 '16 at 14:48
  • @HcN Yes, in the \tbhelp macro, change \texttt{#1} to \texttt{\detokenize{#1}}. That will still not be able to handle % in the input and will break if unmatched braces are in the input. – Steven B. Segletes Mar 03 '16 at 14:56
  • Ok this works now. I have a final request, it seems like it cannot cope with spaces. Is there a fix for that ? also, it is a bit of a shame that it does not use the listings package (so I will have to find another solution to have my code in colour), but this does the trick for what I need. – HcN Mar 03 '16 at 18:08
  • 1
    @HcN Brute force fix is to include space within braces in the argument: \alttxtbrut{Iletai_t_un{ }efoisunejeuneetjolie.txt} – Steven B. Segletes Mar 03 '16 at 19:59