在指导下自动对逐字文本进行连字

在指导下自动对逐字文本进行连字

我想排版逐字文本,使其在某些文本点处可以换行。我知道并且spverbatim可以listings在空格处换行,但有时这还不够。当然,没有连字引擎能够找出某些编程语言的晦涩命令中的连字点,所以我愿意用可能的断点来引导引擎,比如\?-。这些断点处的换行需要一个特殊的连字字符,这样读者才能看到这不是在某个空格处换行。

一张图片胜过千言万语: 在此处输入图片描述

我确信我在 TeXy 文本中看到过类似的东西。

有没有办法实现这样的\magicverb(或者它已经在某个地方实现了?)

图片代码:

\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}

答案1

确保将其保存为 latin1 而不是 utf8(或将 £ 更改为 pdftex 可以理解的单个字符)

在此处输入图片描述

\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}

相关内容