0

I need to write about 200 the following environment, so I was thinking to define it at one place.

Here is my original codes

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

How can I define a new environment codeExample so that I can use it as below to generate the same content as above,

\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}   
drbombe
  • 173
  • 2
    Welcome to TeX.SX! You want an item command to start and end verbatim? Good luck, but that's usually a bad idea... – TeXnician Oct 11 '17 at 12:29
  • Possibly related: https://tex.stackexchange.com/questions/7008/change-hanging-indent-in-description-list – John Kormylo Oct 11 '17 at 13:12
  • @TeXnician Thank you for your comments. Could you please help me to understand why it is a bad idea? Also, I was thinking to put C++ codes into LaTex, besides verbatim, any better idea? – drbombe Oct 11 '17 at 14:10
  • No, unfortunately not. The problem is verbatim, nothing else. Everything except \end{verbatim} is "eaten" by verbatim mode (including the next \item). Putting that into commands is a pretty harmful combination (there are posts about that on this site). I also do not see the need for description here (as you want to produce headings you could manage with counters...). – TeXnician Oct 11 '17 at 14:12
  • 1
    How will codeExample "generate Code 6" from the input/description you provided? – Werner Oct 11 '17 at 19:36
  • @Werner it should be Code 3 text 6 – drbombe Oct 11 '17 at 19:42
  • @drbombe: But how? Do you supply it? What does it mean that "it will generate [it]"? – Werner Oct 11 '17 at 19:44
  • @Werner I was thinking that when we add \item, it will generate Code 3 with Verbatim environment, if we add another \item, it will generate Code 4: and add another \item it will generate Code 5: ... so on and so forth. – drbombe Oct 12 '17 at 02:15

2 Answers2

2

Maybe, your problem can be solved by following code:

\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}
wipet
  • 74,238
  • Almost there! I need to have 6 \item to run your codes. Sometimes I only need 4 and some times that I need 5. How can I fix it? Because sometimes, I only need 2 code examples, and sometimes 1 code examples. – drbombe Oct 11 '17 at 22:41
  • Use environments with different names for such different usage. – wipet Oct 12 '17 at 05:33
0

Rewritten answer now working with an arbitrary number of code blocks, but still no real verbatim mode:

\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}
Skillmon
  • 60,462
  • Thank you so much, we are almost there! My purpose is to copy and paste my C++ codes, so that it an automatically display it with Verbatim setting. Is there any easy way to make this happen? – drbombe Oct 11 '17 at 18:30
  • @drbombe see my edit. Note that it still has some restrictions. – Skillmon Oct 11 '17 at 19:25
  • Thanks @Skillmom. But it doesn't compile on my computer now. I am new to this macro. Your help is highly appreciated! – drbombe Oct 11 '17 at 23:03
  • @drbombe I wrote a new version. If you want, I can enable you to use empty lines without the additional space at the end (but it might lead to inconsistent spacing in edge cases). – Skillmon Oct 12 '17 at 11:25
  • @drbombe I adapted the new version -- just in case you're still interested in this -- to obey spaces (indentation works), but unbalanced braces in a single line lead to an error (so even if they are balanced but within different lines). – Skillmon Oct 14 '17 at 12:58