在特殊的逐字环境中在每行开头插入一个字符

在特殊的逐字环境中在每行开头插入一个字符

如何在逐字环境中的每一行开头插入特殊字符或符号?具体来说,逐字环境有一个限制:它必须是特殊类型(如下所示);它不能是任何标准环境verbatim,例如来自包fancyvrblistings..

我正在开发一个用于格式化电子邮件通信的环境。它基于对标准verbatim环境的修改,但没有该\obeylines部分,请参阅“格式化逐字段落

由于代码主要由 Frank Mittelbach 开发,我不确定什么是最小的工作示例。所以我认为我必须包括整个环境。以下是一个例子:

\documentclass{article}
\usepackage{xstring}
\makeatletter
\newdimen\outerparindent
\def\myverbatim#1{%
  \IfStrEq{#1}{c}{\renewcommand{\verbatim@font}{\slshape\ttfamily}}
  {\renewcommand{\verbatim@font}{\upshape\ttfamily}}%
  \def\do@noligs##1{%
     \catcode`##1\active
     \begingroup
       \lccode`\~`##1\relax
       \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`##1 }}}%
   \outerparindent\parindent
   \def\obeylines{\rightskip=0pt plus 1fil
                  \parindent=\outerparindent
                  \let\par\@@par
                  \leavevmode\indent}%
   \def\@xobeysp{\penalty\z@\mbox{}\space\penalty\z@}%  
  \@verbatim
  \@myverbatimescape\@myverbatimnewline
  \frenchspacing\@vobeyspaces\@xmyverbatim}
\let\endmyverbatim\endverbatim
\begingroup
\catcode`\^^M=\active%
\gdef\@myverbatimnewline{\catcode`\^^M=\active \let^^M\@xmyverbatimnewline}%
\gdef\@xmyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{\@xobeysp}}%
\gdef\@ymyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\gdef\@myverbatimpar ^^M{\par%
                         \vskip\baselineskip%
                         \@ymyverbatimnewline}%
\gdef\@zmyverbatimnewline{\@ifnextchar ^^M{\@zmyverbatimpar}{}}%
\gdef\@zmyverbatimpar^^M{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\endgroup
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=13
|gdef|@myverbatimescape[|catcode`|\|active|let\|@myverbatimbslash]
|gdef|@myverbatimbslash[|@ifnextchar\[|@xmyverbatimbslash][|string\]]
|gdef|@xmyverbatimbslash\[|\]
|catcode`|^^M=|active
|long|gdef|@xmyverbatim#1\end{myverbatim}[|@zmyverbatimnewline#1^^M|vskip-|lastskip|vskip|z@skip|end[myverbatim]]%
|endgroup
\makeatother
\begin{document}
\begin{myverbatim}{c}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\begin{myverbatim}{n}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\end{document}

当前输出: 在此处输入图片描述

(目标:我想在第一段的每一行的开头插入一个特殊符号,如>或(即在倾斜文本的每一行的开头))|

更具体地说:我如何修改环境,使得如果环境的参数myverbatim是字符c,则每行前面都有一个符号,例如>,如果参数不是,c则不会在行首插入任何符号。

我研究了 David 对这篇文章的回答“环境在每行输出的开头打印相同的字符“但我无法让它适用于我的情况。也许有一个更简单的解决方案?

(也可以看看:如何格式化电子邮件对话?

答案1

我发现了一个使用该mdframed包的可能的解决方法:

\documentclass{article}
\usepackage{mdframed,xstring}
\makeatletter
\newdimen\outerparindent
\newcommand{\myverbatim}[1]{%
  \IfStrEq{#1}{c}{
    \begin{mdframed}[linewidth=1,topline=false,rightline=false,bottomline=false]
      \renewcommand{\verbatim@font}{\slshape\ttfamily}
      \gdef\end@myverbatim{\end{mdframed}}
}
  {\renewcommand{\verbatim@font}{\upshape\ttfamily}
    \gdef\end@myverbatim{}
}%
  \def\do@noligs##1{%
     \catcode`##1\active
     \begingroup
       \lccode`\~`##1\relax
       \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`##1 }}}%
   \outerparindent\parindent
   \def\obeylines{\rightskip=0pt plus 1fil
                  \parindent=\outerparindent
                  \let\par\@@par
                  \leavevmode\indent}%
   \def\@xobeysp{\penalty\z@\mbox{}\space\penalty\z@}%  
  \@verbatim
  \@myverbatimescape\@myverbatimnewline
  \frenchspacing\@vobeyspaces\@xmyverbatim}

\let\endmyverbatim\endverbatim
\begingroup
\catcode`\^^M=\active%
\gdef\@myverbatimnewline{\catcode`\^^M=\active \let^^M\@xmyverbatimnewline}%
\gdef\@xmyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{\@xobeysp}}%
\gdef\@ymyverbatimnewline{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\gdef\@myverbatimpar ^^M{\par%
                         \vskip\baselineskip%
                         \@ymyverbatimnewline}%
\gdef\@zmyverbatimnewline{\@ifnextchar ^^M{\@zmyverbatimpar}{}}%
\gdef\@zmyverbatimpar^^M{\@ifnextchar ^^M{\@myverbatimpar}{}}%
\endgroup
\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=13
|gdef|@myverbatimescape[|catcode`|\|active|let\|@myverbatimbslash]
|gdef|@myverbatimbslash[|@ifnextchar\[|@xmyverbatimbslash][|string\]]
|gdef|@xmyverbatimbslash\[|\]
|catcode`|^^M=|active
|long|gdef|@xmyverbatim#1\end{myverbatim}[|@zmyverbatimnewline#1^^M|vskip-|lastskip|vskip|z@skip|end@myverbatim|end[myverbatim]]%
|endgroup
\makeatother

\begin{document}
\begin{myverbatim}{c}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}
\begin{myverbatim}{n}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et venenatis at, tristique in tortor
\end{myverbatim}

\end{document}

输出:

在此处输入图片描述

我认为它看起来还不错。此外,该mdframed包允许环境跨越分页符:)

答案2

您是否明确地依赖于逐字环境,还是仅依赖于逐字环境的功能?如果是后者,下面的示例将以逐字方式格式化文本,遵循换行和边距。使用此[c]选项,它会在文本左侧放置一个垂直条,虽然不是每行第一列中的一个字符,但似乎可以满足您以某种方式识别先前对应关系的基本需求。它还接受一个可选参数,可以进一步定制结果的外观。[感谢 Joseph Wright保持(和控制)扩展性用于\detokenize指针]

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{tabularx}
\newcommand\display[3][]{%
\noindent\tabcolsep=3pt\renewcommand\arraystretch{0}\arrayrulewidth=.5ex%
\if c#2%
  \begin{tabularx}{\textwidth}{|X}%
  \raggedright\color{blue}\ttfamily\slshape#1\detokenize\expandafter{#3}%
  \end{tabularx}%
\else%
  \tabcolsep=0pt%
  \begin{tabularx}{\textwidth}{X}%
  \raggedright\ttfamily#1\detokenize\expandafter{#3}%
  \end{tabularx}%
\fi
}
\begin{document}
\noindent Textwidth bar
\raggedright
\noindent\rule{\textwidth}{0.5ex}
\vspace{1em}

\display{n}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\display[\itshape\color{red}]{n}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\display{c}{%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed orci
quis urna volutpat tincidunt. Praesent ultrices commodo auctor. Nunc vel
nulla sed arcu aliquet faucibus quis nec ante. Nam elementum neque vitae
sem fringilla a euismod eros malesuada. Duis orci eros, tincidunt et
venenatis at, tristique in tortor and other \verbatim characters^ $ &
\\ etc.%
}

\end{document}

在此处输入图片描述

相关内容