[Tex/LaTex] Automatic hyphenation in verbatim text, with guidance

hyphenationline-breakingverbatim

I would like to typeset verbatim text that can have linebreaks on certain text points. I know that spverbatim and listings can break at spaces, but sometimes this is not enough. Of course, no hyphenation engine can figure out hyphenation points within obscure commands of some programming language, so I am willing to guide the engine with possible break points, say \?-. Linebreaks at these break points would need a special hyphenation character, so the reader sees that this is not a break at some space.

A picture is worth a thousand words:
enter image description here

I am pretty sure I have seen something like that in TeXy looking texts.

Is there a way to implement such a \magicverb? (Or is it already implemented somewhere?)

Code for the pic:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage[T1]{fontenc}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{spverbatim}
\usepackage{listings}
\lstset{language=[LaTeX]TeX,aboveskip=-\ht\strutbox,belowskip=-\ht\strutbox}

\usepackage{dingbat}
\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begingroup
\small
\centering
\begin{tabular}{>{\raggedright\arraybackslash}p{10.8cm}|>{\raggedright\arraybackslash}p{3cm}|}
  approach & output \\
  \midrule
  \verb!\spverb{|\fontfamily{LinuxLibertineO-OsF} \selectfont|}! 
  & \spverb|\fontfamily{LinuxLibertineO-OsF} \selectfont|
  \\
  \addlinespace
\begin{verbatim}
\begin{lstlisting}[breaklines]
\fontfamily{LinuxLibertineO-OsF} \selectfont
\end{lstlisting}
\end{verbatim}
 & 
\begin{lstlisting}[breaklines]
\fontfamily{LinuxLibertineO-OsF} \selectfont
\end{lstlisting} \\
\addlinespace
\begin{verbatim}
\magicverb{\fontfamily{Linux\?-LibertineO-\?-OsF} \select\?-font}
\end{verbatim}
 & \texttt{\textbackslash{}fontfamily\{Linux\carriagereturn{}\hspace{0pt}LibertineO-\carriagereturn{}\hspace{0pt}OsF\}\textbackslash{}select\-font}
\end{tabular}
\endgroup

\bigskip
\noindent{}Of course the last output was faked with
\begin{spverbatim}
\texttt{\textbackslash{}fontfamily\{Linux\carriagereturn{}
\hspace{0pt}LibertineO-\carriagereturn{}
\hspace{0pt}OsF\}\textbackslash{}select\-font}
\end{spverbatim}

\end{document}

Best Answer

Make sure you save this as latin1 not utf8 (or change £ to a single character as understood by pdftex)

enter image description here

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage[T1]{fontenc}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{spverbatim}
\usepackage{listings}
\lstset{language=[LaTeX]TeX,aboveskip=-\ht\strutbox,belowskip=-\ht\strutbox}

\usepackage{dingbat}
\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begingroup
\small
\centering
\begin{tabular}{>{\raggedright\arraybackslash}p{10.8cm}|>{\raggedright\arraybackslash}p{3cm}|}
  approach & output \\
  \midrule
  \verb!\spverb{|\fontfamily{LinuxLibertineO-OsF} \selectfont|}! 
  & \spverb|\fontfamily{LinuxLibertineO-OsF} \selectfont|
  \\
  \addlinespace
\begin{verbatim}
\begin{lstlisting}[breaklines]
\fontfamily{LinuxLibertineO-OsF} \selectfont
\end{lstlisting}
\end{verbatim}
 & 
\begin{lstlisting}[breaklines]
\fontfamily{LinuxLibertineO-OsF} \selectfont
\end{lstlisting} \\
\addlinespace
\begin{verbatim}
\magicverb{\fontfamily{Linux\?-LibertineO-\?-OsF} \select\?-font}
\end{verbatim}
 & 
\catcode`\£\active
\def£{\discretionary{\carriagereturn}{}{}}
\begin{verbatim}
\fontfamily{Linux£LibertineO-£OsF} \select£font
\end{verbatim}
\end{tabular}
\endgroup

\bigskip
\noindent{}Of course the last output was faked with
\begin{spverbatim}
\texttt{\textbackslash{}fontfamily\{Linux\carriagereturn{}
\hspace{0pt}LibertineO-\carriagereturn{}
\hspace{0pt}OsF\}\textbackslash{}select\-font}
\end{spverbatim}

\end{document}
Related Question