如何将 Verbatim 转变为 itemize 环境?

如何将 Verbatim 转变为 itemize 环境?

我正在设计一类文档,这些文档始终具有相同的元素,其中包括两个无序列表,其中的内容需要手动复制粘贴到文档中。由于这将由那些只使用基本的 MSWord 的人使用,因此我试图在 cls 文件中隐藏尽可能多的复杂内容。

我迄今为止最好的尝试是这样的:

.cls文件

\NeedsTeXFormat{LaTeX2e}

\ProvidesClass{formtemplate}[2018/08/23 XeLaTeX-Class]
\RequirePackage{xcolor}
\RequirePackage{scrbase}
\DefineFamily{formtemplate}
\DefineFamilyMember{formtemplate}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}%
\PassOptionsToClass{\CurrentOption}{babel}}
\FamilyProcessOptions{formtemplate}\relax

\LoadClass[usegeometry]{scrartcl}
\RequirePackage[includehead,margin=0.7cm]{geometry}
\RequirePackage{graphicx}
\RequirePackage{babel}
\RequirePackage{fancyvrb}
\RequirePackage{etoolbox}
\RequirePackage{enumitem}
\RequirePackage{fontspec}
\RequirePackage{tabu}
\RequirePackage{forloop}

\setmainfont[BoldFont={CalibriB.ttf}]{Calibri.ttf}
\setmonofont[BoldFont={CalibriB.ttf}]{Calibri.ttf}
\geometry{headheight=0mm,headsep=-5mm}

\newcommand{\Left}[1]{%
    \fontsize{18pt}{18pt}\selectfont\textbf{#1}%
}
\newlength{\wL}\newlength{\wLM}\newlength{\wM}\newlength{\wMR}\newlength{\wR}
\newcommand{\Prep}{%
    \settowidth{\wL}{\Left{Contact: }}
    \settowidth{\wLM}{\Left{Requirements: }}
    \deflength{\wM}{\wLM-1.05\wL}
    \deflength{\wMR}{\textwidth-1.05\wL}
    \deflength{\wR}{\textwidth-1.02\wLM}
}
\newcommand{\What}[1]{%
    \csgdef{whatval}{\expandafter#1}%
}

\setlist{noitemsep,leftmargin=8pt,topsep=0pt,labelsep=0.5ex}

\newcounter{reqc}
\setcounter{reqc}{1}
\newcounter{reqi}
\newcommand{\setreq}[1]{%
    \csgdef{req\the\value{reqc}}{#1}%
    \stepcounter{reqc}%
}
\newenvironment{Requirements}{%
    \VerbatimEnvironment%
    \renewcommand{\FancyVerbFormatLine}[1]{\setreq{##1}}%
    \begin{BVerbatim}
}{%
    \end{BVerbatim}
}
\newcommand{\reqval}{%
    \begin{minipage}[t]{0.99\wR}%
    \begin{itemize}%
    \fontsize{12pt}{12pt}\selectfont%
    \forloop{reqi}{1}{\value{reqi}<\value{reqc}}{%
        \item{\csuse{req\the\value{reqi}}}%
    }%
    \end{itemize}%
    \vspace{0.5ex}%
    \end{minipage}%
}

\newcommand{\Show}{%
    \noindent%
    $\;$\\
    \begin{tabu} {@{}p{\wL}@{}p{\wM}@{}p{\wR}@{}}%
        \Left{What:} & \multicolumn{2}{@{}r@{}}{\fontsize{15pt}{15pt}\selectfont\textbf{\whatval}}\\\tabuphantomline\tabucline{3-}
        \multicolumn{2}{@{}l@{}}{\parbox[t][][t]{\wLM}{\Left{Requirements:}}} & \reqval\\\tabuphantomline\tabucline{3-}
    \end{tabu}%
}

.tex文件

\documentclass[paper=a5,fontsize=10pt,pagesize,en]{formtemplate}
\begin{document}
\Prep
\What{Some kind of labeling}
\begin{Requirements}
Some normal item point
A longer item point that might require multiple requirement lines but does not contain hyphens
A line with a pesky error-causing hyphen
Another normal line
Another hyphen-related erroring line
Last line
\end{Requirements}
\Show
\end{document}

当前的问题在于它忘记了连字符是什么。

以下是日志中看似相关的部分:

! Undefined control sequence.
\requirement3 ->A line with a pesky error-
                                          causing hyphen
l.14 \Show

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\requirement5 ->Another hyphen-
                               related erroring line
l.14 \Show

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

编辑:连字符不是唯一消失的字符。逗号也是如此。分号会产生错误消息,但可以正常显示。

答案1

这是一个伪逐字方法,它不会禁用 TeX 的换行功能,但不允许使用某些特殊字符(即\{}):

\documentclass{article}

\usepackage{soul}

\makeatletter
\newdimen\VBI@indent
\newbox\VBI@box
\newtoks\VBI@everypar
\newenvironment{verbitemize}{%
  \let\VBI@tilde~% Save the tilde
  \setbox\VBI@box\hbox{\hss$\bullet$\VBI@tilde\hss}% Save the bullet box
  \ifdim\parindent > \wd\VBI@box
    \setbox\VBI@box\hb@xt@\parindent{\unhbox\VBI@box}%
  \fi
  \VBI@indent\wd\VBI@box% Measure the hanging indentation
  \parindent\z@% Remove paragraph indentation
  \obeylines% Make ^^M active
  \VBI@everypar{% Add a few tokens to \everypar:
    \copy\VBI@box% $\bullet$^
    \hangindent\VBI@indent% Hanging indentation
    \hangafter\@ne}%
  \expandafter\futurelet\expandafter\VBI@tokn
    \expandafter\VBI@check@minipage\the\everypar\relax\@nnul
  \def\VBIdo{% List of special characters to be allowed in this speudo-verbatim mode
    \do\$\do\&\do\#\do\^\do\_\do\~%
  }% BEWARE! \do\\, \do\{, and \do\} CANNOT be used here
  \let\do\@makeother\VBIdo
}{}
\def\VBI@check@minipage#1\@nnul{%
  \ifx\VBI@tokn\@minipagefalse
    \everypar{\@minipagefalse\the\VBI@everypar
                   \everypar{\the\VBI@everypar}}%
  \else
    \everypar\expandafter{\the\everypar\the\VBI@everypar}%
  \fi}
\makeatother

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.

\begin{verbitemize}
  Some normal item point
  A longer item point that might require multiple requirement lines but does not contain hyphens
  A line with a \st{pesky error-causing hyphen} not anymore \texttt{=D}
  Another normal line
  Another hyphen-related erroring line
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  A line with special characters: $ & # ^ _ ~
  Last line
\end{verbitemize}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.

\end{document}

enter image description here

在排版之前,环境会进行一些逐字设置:

首先,它保存~\VBI@tilde允许用户写入~

  \let\VBI@tilde~%

然后创建一个\VBI@box包含子弹的盒子:

  \setbox\VBI@box\hb@xt@\parindent{\hss$\bullet$\VBI@tilde\hss}%

并使用其宽度来测量悬挂压痕:

  \VBI@indent\wd\VBI@box

这里我们不想使用正常的段落缩进:

  \parindent\z@

现在我们激活新线路,因此每条线路都会插入一个\partoken

  \obeylines

然后在每一行之后\VBI@everypar都会执行标记列表:

  \VBI@everypar{%
    \copy\VBI@box
    \hangindent\VBI@indent
    \hangafter\@ne}%

但在将其插入之前,\everypar我们需要检查是否在 内minipage。这minipage会清除\everypar标记列表,因此我们需要检查。我使用了一个宏\VBI@check@minipage,它将查看\everypar标记列表并检查是否\@minipagefalse存在。

现在我们只需要使特殊字符可用而无需转义:

  \def\VBIdo{\do\$\do\&\do\#\do\^\do\_\do\~}%
  \let\do\@makeother\VBIdo

这将允许$&、、和。如果您不想要其中任何一个,只需删除该条目。请注意#,和不应在此处使用,否则环境将找不到。^_~\{}\end{verbitemize}

编辑:

已修复在 s 内部工作的情况以及小于项目符号的minipage情况。\parindent

答案2

可能是这样的。我verbitemize使用包的构造创建了一个新环境,verbatimbox并将其用作!换行符。

\documentclass{article}
\usepackage{verbatimbox}
\catcode`!=\active
\def!{\newline\mbox{}\hspace{2em}}
\catcode`!=12 %
\newcommand\vbsetup{$\bullet$~}

\makeatletter
\newenvironment{verbitemize}{%
\catcode`!=\active %
\setcounter{VerbboxLineNo}{-1}%
\let\my@par\par%
\def\verbatim@processline{%
{\addtocounter{VerbboxLineNo}{1}%
\@tmp\setbox0=\hbox{\@tmp\the\verbatim@line}%
\hsize=\wd0 \the\verbatim@line\my@par}}%
\verbatim\verbbox@inner%
}
{%
\endverbatim%
\global\def\@tmp{}%
}
\makeatother

\begin{document}
\noindent Here is normal text!

\begin{verbitemize}[\vbsetup]
This is a test
Now another.
Here I insert! manual line breaks! with an! exclamation point.
Finally, the last line
\end{verbitemize}

\noindent Back!to!normal!text!
\end{document}

enter image description here

答案3

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{VerbItem}{%
    \renewcommand*\thelstnumber{\textbullet}%
    \lstset{basicstyle=\ttfamily\small,breakatwhitespace,breaklines,
            numbers=left,numbersep=2pt,xleftmargin=0.75em}}{}
\begin{document}

\begin{VerbItem}
Some normal item point
A longer item point that might require multiple requirement lines but does not contain hyphens
A line with a pesky error-causing hyphen
Another normal line
Another hyphen-related erroring line
Last line
\end{VerbItem}

\noindent
foo bar baz

\end{document}

enter image description here

相关内容