我如何定义一个新的环境并传递项目的不同条件?

我如何定义一个新的环境并传递项目的不同条件?

我需要写大约 200 个以下环境,所以我想在一个地方定义它。

这是我的原始代码

\begin{description}
    \item[Description:] \ \\  text 1 here
    \item[Analysis: ] \ \\ text 2 here
    \item[Related Problems:] \ \\ text 3 here
    \item[Code 1:] \
    \begin{verbatim}
    text 4 here
    \end{verbatim}
    \item[Code 2:] \ 
    \begin{verbatim}
    text 5 here
    \end{verbatim}
\end{description}

我如何定义一个新的环境,codeExample以便我可以像下面一样使用它来生成与上面相同的内容,

\begin{codeExample}
    \item text 1 here
    \item text 2 here
    \item text 3 here
    \item text 4 here
    \item text 5 here
    \item text 6 here (it will generate Code 6)
\end{codeExample}   

答案1

也许,你的问题可以通过以下代码解决:

\documentclass[a4paper]{scrreprt}

\newenvironment {codeExample} {\codeexampleA}{\end{description}}
\def\codeexampleA #1\item #2\item #3\item #4\item {%
    \begin{description}
    \item[Description:] \ \\ #2
    \item[Analysis: ] \ \\ #3
    \item[Related Problems:] \ \\ #4
    \bgroup\redefverbatim\codeexampleB
    \item[Code 1:] \
    \begin{verbatim}
}
\def\codeexampleB{\egroup
    \item[Code 2:] \ 
    \bgroup\redefverbatim{\egroup\item[Code 3:]}
    \begin{verbatim}
}
\def\redefverbatim#1{\escapechar=-1
   \expandafter\def\csname @xverbatim\expandafter\endcsname
   \expandafter##\expandafter 1\string\\item{##1\end{verbatim}#1}%
}

\begin{document}

\begin{codeExample}
    \item text 1 here
    \item text 2 here
    \item text 3 here
    \item 
text 4 here $#!$ %{}
more lines
    \item 
text 5 here
more lines #%^
    \item text 6 here (it will generate Code 6)
\end{codeExample}

\end{document}

答案2

重写的答案现在可以处理任意数量的代码块,但仍然没有真正的逐字模式:

\documentclass[]{article}

\usepackage{etoolbox}% for \csdef and \csuse
\usepackage{expl3}% for the verbatim like function

\makeatletter
\csdef{cE@1}{Description:}
\csdef{cE@2}{Analysis:}
\csdef{cE@3}{Related Problems:}
\csdef{cE@ge4}#1{Code #1:}
\newcount\cE@count

\newenvironment*{codeExample}%
{%
  \cE@clear@tl%
  \let\itemBAK\item%
  \def\item{%
    \advance\cE@count by 1\relax%
    \ifnum\cE@count<4\relax%
      \itemBAK[\csuse{cE@\the\cE@count}]\mbox{}\\%
    \else%
      \catcode`\%=12\relax%
      \obeylines% make newlines work
      \obeyspaces% make indentation work
      \expandafter\cE@verb@args%
    \fi}%
  \begin{description}
}{%
  \end{description}%
}

\ExplSyntaxOn
\tl_new:N \l_cE_verb_content_tl
\seq_new:N \l_cE_verb_content_seq
\cs_new:Npn \cE@clear@tl { \tl_clear:N \l_cE_verb_content_tl } % name against L3 coding conventions
\cs_new:Npn \cE@verb@args #1 \end #2 { % name against L3 coding conventions
  \tl_put_right:Nn \l_cE_verb_content_tl { #1 }
  \tl_if_eq:nnTF { #2 } { codeExample }
    {
      \cE_verb_print:N \l_cE_verb_content_tl
      \end{codeExample}
    }
    { 
      \tl_put_right:Nn \l_cE_verb_content_tl { \end { #2 } }
      \cE@verb@args
    }
  }
\cs_new:Npn \cE_verb_print:N #1 {
  \seq_set_split:NnV \l_cE_verb_content_seq { \item } #1
  \seq_map_function:NN \l_cE_verb_content_seq \cE_verb_print_one_item:n
}
\cs_new:Npn \cE_verb_print_one_item:n #1 {
  \itemBAK[\csuse{cE@ge4}{\int_eval:n {\the\cE@count-3}}]
  \group_begin:
  \regex_split:nnN  { \r } { #1 } \l_tmpa_seq
  %\seq_remove_all:Nn \l_tmpa_seq {} % COMMENT OUT THIS FOR EMPTY LINES
  \seq_map_function:NN \l_tmpa_seq \cE_print_verb_lines:n
  \exp_args:NNV \str_set:Nn \l_tmpa_str \l_tmpa_tl
  \group_end:
  \advance\cE@count by 1\relax
  \vspace{-\baselineskip}% Undo the trailing newline of a code block
}
\cs_new:Npn \cE_print_verb_lines:n #1 {
  \group_begin:
  \str_set:Nn \l_tmpa_str { #1 }
  \texttt{ \mbox{}\\\null \str_use:N \l_tmpa_str }
  \group_end:}
\ExplSyntaxOff
\makeatother

\begin{document}
\begin{codeExample}
    \item text 1 here
    \item text 2 here
    \item text 3 here
    \item% Th_s  is \ not \completely % verbatim, but ^I changed the catcode of %
      newline is treated correctly
      and long lines get broken automatically -- but the newline starts without any indent
      but leading spaces don't get removed (lucky that C++ doesn't need indentation like python does)

      empty lines are not removed -- if you don't want this the last line will have a newline too much
    \item text 5 here % still no comments
    \item[foo] text 6 here (it will generate Code 6)% comments still don't work
    \item it works for an arb^trary n_mber of codblocks
    \item and only the correct \end{with} codeExample as argument terminates it
\end{codeExample} 
Another example with less code blocks:
\begin{codeExample}
  \item less items% comments work
  \item are
  \item possible
  \item code block starts _here
\end{codeExample}
\end{document}

相关内容