Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

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}
share|improve this answer
    
It's not so easy to find a suitable character that works with utf8, I think I settle with +. Thanks. –  mafp Jan 8 '13 at 0:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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