如何制作打印逐字文本的宏?

如何制作打印逐字文本的宏?

我现在正在尝试使用 verbatim 进行自动换行,并且我有此设置。

\usepackage{fancyvrb}
\usepackage{fvextra}


\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
text text text text text \macro{text}
\end{Verbatim}

我现在的设置有点麻烦,我现在想做的是编写一个宏,让我可以自动输入所有这些内容,如下所示:

\newcommand{\bigvrb}[1]{
\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
#1
\end{Verbatim}}

这样我就可以更轻松地完成所有事情,如果我想更改格式,我可以批量编辑。问题是,它只是将其排版为,#1因为逐字工作就是这样的。我试过使用\expandafter类似的东西,但没有什么效果。你们有什么想法吗?非常感谢!!!

答案1

fancyvrb包允许您定义自己的环境。如果您使用它,您只需更改定义:

\documentclass[]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\DefineVerbatimEnvironment{MyVerbatim}{Verbatim}{breaklines=true,numbers=left,breakafter=\},breakbefore=\\}

\begin{document}

\begin{MyVerbatim}
text text text text text \macro{text}
\end{MyVerbatim}

\end{document}

答案2

无法推荐使用具有无界参数语法的宏来生成多行逐字材料:

处理源代码缩进的问题会引起混淆,因为源代码中的逐字缩进空间被标记化,因此在输出中显示为水平空间/可见空间。

为了展示一些问题/怪异之处,下面的示例提供了一个宏\bigvrb-showspaces=true应用该选项,以便您可以更好地看到旨在缩进的空格的处理,只是为了使源代码更具可读性:

\RequirePackage{xparse}
\documentclass[a4paper]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\NewDocumentCommand\bigvrb{}{%
  \begingroup
  \catcode`\^^I=12 %
  %\endlinechar=`\^^M
  \newlinechar=\endlinechar
  \innerbigvrb
}%
\begingroup
\catcode`\^^A=14\relax
\catcode`\%=12\relax
\catcode`\{=12\relax
\catcode`\}=12\relax
\catcode`\(=1\relax
\catcode`\)=2\relax
\catcode`\/=0\relax
\catcode`\\=12\relax
/catcode`/^^M=12/relax^^A
/csname @firstofone/endcsname(^^A
  /endgroup^^A
  /NewDocumentCommand/innerbigvrb(+v)(^^A
    /scantokens(^^A
      \endgroup^^A
      \begin{Verbatim}[showspaces=true, breaklines=true,numbers=left,breakafter=\},breakbefore=\\]^^M^^A
      #1^^M^^A
      \end{Verbatim}^^M%^^A
    )^^A
  )^^A
)%

\pagestyle{empty}
\parindent=0ex
\parskip=.25\baselineskip

\begin{document}
\enlargethispage{9cm}\vspace*{-4.5cm}%

\noindent\hrulefill

This looks okay:

\bigvrb{ABC
DEF}

\noindent\hrulefill

This has a visible comment-char and the spaces for indenting the code are visible also:

\bigvrb{%
  ABC%
  DEF%
}
\noindent\hrulefill

The spaces for indenting the code are visible. Besides this you have four
linebreaks \emph{inside} the verbatim-argument---the linebreak before the
first line (\verb*|  ABC|) and the linebreak after the last line
(\verb*|  DEF|) each yields an empty line:

\bigvrb{
  ABC
  DEF
}

\noindent\hrulefill

Even more spaces are used for indenting the code. They all are visible:

\begin{itemize}
\item Some text of an item of an \verb|itemize|-environment.
      That item also contains a \verb|\bigvrb|-command whose
      result is here:
      \bigvrb{ABC
              DEF}
\end{itemize}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way, keeping in mind that
curly braces must be balanced with \verb|\bigvrb|'s argument if not using delimiter-syntax:

\let\MyClosebrace=\}
\bigvrb{First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\MyClosebrace, breakbefore=\\]
Second environment}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way,
using delimiter-syntax---as verbatim-arg-delimiter \verb|>| is chosen:

\bigvrb>First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\}, breakbefore=\\]
Second environment>


\noindent\hrulefill

\textbf{Summa summarum:}

Undelimited-argument-syntax with multiline-verbatim-material is
confusing as 
\begin{itemize}
\item you cannot have the usual code-indentation and linebreaking.
\item you cannot easily use the command \verb|\bigverb|
for verbatimized typesetting of the line \verb|\end{Verbatim}|.
\item you may need to take care of braces being balanced in
\verb|\bigvrb|'s argument if you don't use delimiter-syntax.
\end{itemize}

\end{document}

在此处输入图片描述

当然\bigvrb,上面的例子的目的是通过在(暂时)切换到 verbatim-catcode-régime 后读取和标记 .tex-input 来获取其参数。

因此\bigvrb在获得其论据的情况下不起作用

  • 通过将其作为宏定义的一部分传递,该宏定义在非逐字猫码制度下被标记化
  • 通过将其从另一个宏传递,在执行该宏期间,该参数在非逐字 catcode 机制下被标记化
  • \the当寄存器的内容在非逐字 catcode 机制下被标记化时,通过 -expansion 将其从标记寄存器传递过来
  • ETC。

将其作为移动论证的组成部分可能会产生意想不到的效果。

根据经验法则,您可以\bigvrb在文档文本中使用它,但不能在宏定义/其他宏的参数中使用它。因此它的用途非常有限。

答案3

tokcycle方法旨在以连续的方式处理输入流中的标记(和组)。因此,它可以程序仿真大多数情况下都是逐字逐句的(例外是分组标记{}必须平衡)

但正如我们在 MWE 中看到的,像 这样的特殊字符{#$%^&\_}都得到了处理。我进一步设置了循环来将段落解释为,\par但将换行符解释\\为实际换行符。这些行为当然可以根据需要重新编程。在 MWE 中,空格标记默认为空格,但可以使用可选参数将其设置为其他内容,例如可见空格。

重要的是,这\altdetokenize消除了宏名后面难看的空格,当然,当宏后面跟着字母时,必须保留空格。

这种方法不支持行号。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle}
\newif\ifmacro
\newcommand\altdetokenize{\begingroup\catcode`\%=12\altdetokenizeAUX}
\newcommand\altdetokenizeAUX[2][ ]{\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    {#1\hspace{0pt plus .3em minus .3ex}}
    {#2}%
  \unskip
\endgroup}

\begin{document}
\altdetokenize{This is a \relax test to check
  out line breaking and verbatim {#$%^&\_} as well as that ugly
  space after \macros. Like \a\b\c

And what about paragraphs?\\
And linebreaks?
}

\altdetokenize[\textvisiblespace]{This is a \relax test to check
  out line breaking and verbatim {#$%^&\_} as well as that ugly
  space after \macros. Like \a\b\c

And what about paragraphs?\\
And linebreaks?
}
\end{document}

在此处输入图片描述

相关内容