句法

句法

这是该问题的后续问题:如何在环境中排版命令及其文字等效项?

显然,处理环境也很有用。我想象如下:

句法

\begin{environment}{env_name}{env_description}
\begin{env_name}
some example text
\end{env_name}
\end{environment}

期望输出

输出结果如下:

在此处输入图片描述

伪代码

\documentclass{article}
\usepackage{fontspec}
\usepackage{menukeys}
\usepackage{xparse}
\usepackage{booktabs} % Adds support for \toprule, \midrule, \bottomrule
\usepackage{array} % https://tex.stackexchange.com/a/4816/13552
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}
\setlength\parindent{0pt}

% Some flipped out function called "environment" goes here:
% \NewEnviron is what I would normally use to capture the body in \BODY
% \NewDocumentEnvironment is what egreg used for the command solution.


\begin{document}

\begin{environment}{mytab}{This environment is a wrapper for the standard latex table environment. This environment is a wrapper for the standard latex table environment. You can optionally adjust the alignment of columns by adding parameters. (e.g. \string$c\string^c\string^c)}
\begin{mytab}
\headrowstart
col1 & col2 & col3 \\
\headrowend
dat1 & dat2 & dat3 \\
dat4 & dat5 & dat6 \\
\end{mytab}
\end{environment}

\end{document}

更新 27-04-2015

我在序言中添加了以下代码:

\ExplSyntaxOn

\NewDocumentEnvironment{environment}{vvv}
 {
  \tl_set:Nn \l_env_argument_i_tl { #1 }
  \tl_set:Nn \l_env_argument_ii_tl { #2 }
  \tl_set:Nn \l_env_argument_iii_tl { #3 }
  \par\vspace{\baselineskip}
  \noindent
  \bgroup\ttfamily
  \l_env_argument_i_tl\par
  \ignorespaces
  \egroup
  \par\nopagebreak
  \noindent
  \l_env_argument_iii_tl \par\noindent % Description
  \textbf{Example:~}\par
    \texttt
   {
  \string\begin\string{\l_env_argument_i_tl\string}\l_env_argument_ii_tl\\
  \string\end\string{\l_env_argument_i_tl\string}
   }
  \\*
 }
 {
  \tl_set_rescan:NnV \l_env_argument_tl {} \l_env_argument_i_tl
  \tl_if_blank:VF \l_env_argument_ii_tl
   {
    \tl_set_rescan:NnV \l_env_temp_tl {} \l_env_argument_ii_tl
    \tl_put_right:Nx \l_env_argument_tl { { \exp_not:V \l_env_temp_tl } }
   }\\*
 }
\tl_new:N \l_env_argument_tl
\tl_new:N \l_env_argument_i_tl
\tl_new:N \l_env_argument_ii_tl
\tl_new:N \l_env_argument_iii_tl
\tl_new:N \l_env_temp_tl
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }
\ExplSyntaxOff

输出应该是文字代码的示例和排版时的示例。

在此处输入图片描述

自动插入代码的问题:

%\begin{environment}{<environment>}{<any parameters>}{<description>}
\begin{environment}{tabular}{{ccc}}{This is the standard latex environment for tabular data.}
% \begin{tabular}{ccc} <-- This gets automatically inserted
col1 & col2 & col3 \\
dat1 & dat2 & dat3 \\
% \end{tabular} <-- This gets automatically inserted
\end{environment}

由于难以捕获环境内的文本,我正在考虑的另一种选择是将表格数据放入 arg3 并将描述移动到环境主体中(只需交换它们)。如果我这样做,我仍然会遇到需要添加\begin{+ arg1 + }+ arg2 + arg3 + \end{+ arg1 +的问题}。我不确定如何做到这一点。

答案1

我没有使用 OP 的代码,但我在这里展示了如何在一个环境中使用filecontentsetoolbox和包来排版一些代码并显示源代码。verbatimbox

已编辑,使用语法将其包含在单一环境中

\begin{enviro}{name}{description}
 <code>
\end{enviro}

我使用自己的简单格式设置输出,但可以根据用户的格式进行更改。

我应该指出,我曾经\verbfilebox设置过代码列表,这样我就可以得到\fbox结果,并防止代码中间出现分页符。如果想要编写能够分页的代码(使用完整\textwidth列表),可以使用\verbfilenobox宏。

\documentclass[12pt]{article}
\usepackage{filecontents,verbatimbox}
\usepackage{etoolbox}
\parindent 0pt
\makeatletter
\newenvironment{enviro}[2]
{\gdef\environame{#1}\gdef\envirodescription{#2}%
  \@tempswafalse\filec@ntents{tmp}}
{\endfilecontents}
\makeatother
\newcommand\setenviro{
  \textbf{\Large\environame:}\par\bigskip
  \envirodescription\par\bigskip
  \textbf{Example:} \input{tmp}\par\bigskip
  \textbf{Code:}
  \verbfilebox[\footnotesize]{tmp}
  \fbox{\theverbbox}
}
\AfterEndEnvironment{enviro}{\setenviro}
\begin{document}
\begin{enviro}{tabular}{This is the standard latex environment for 
  tabular data.}
\begin{tabular}{|c|c|}
\hline
My & tabular\\
\hline
is & here\\
\hline
\end{tabular}
\end{enviro}
\end{document}

在此处输入图片描述

答案2

怀疑这是否是原贴作者想要的,但时不时地从(几乎)第一原则出发做点什么,并说服自己,多年前我花了很多时间阅读 Stephan von Bechtolsheim 的TeX 实践没有完全浪费。

请注意,使用\scantokens假定已etex使用。

\documentclass[border=10]{standalone}

\edef\vbatcatcode{\the\catcode`\@}
\catcode`\@=11

\def\vb@other{11}
\def\vb@active{13}

\def\vb@makeactive#1{\catcode`#1=\vb@active\relax}
\def\vb@makeother#1{\catcode`#1=\vb@other\relax}

\def\vb@dospecials{\let\vb@do=\vb@makeother%
\vb@do\\\vb@do\{\vb@do\}\vb@do\$\vb@do\#\vb@do\%\vb@do\^\vb@do\&\vb@do\~\vb@do\_}

{\catcode`\|=\catcode`\\
\catcode`\[=\catcode`\{
\catcode`\]=\catcode`\}
\vb@makeactive\^^M%
\vb@makeother\{%
\vb@makeother\}%
\vb@makeother\\%
|long|gdef|vb@collect^^M#1^^M\end{vb}[|vb@collected[#1]]%
]

{\vb@makeactive\ %
\vb@makeactive\^^M%
\vb@makeactive\^^I%
\gdef\vb@dospaces{\vb@makeactive\ \def {\vb@typesetspace}}%
\gdef\vb@doreturns{\vb@makeactive\^^M\def^^M{\vb@typesetreturn}}%
\gdef\vb@dotabs{\vb@makeactive\^^I\def^^I{\vb@typesettab}}%
\gdef\vb@returnspacing{%
\vb@makeactive\ \def {\space}%
\vb@makeactive\^^M\def^^M{\space}%
\vb@makeactive\^^I\def^^I{\space}}%
} 

\def\vb@typesetspace{\ }
\long\def\vb@typesetreturn{\par\leavevmode}
\def\vb@typesettab{\ \ \ }

\def\vb#1#2{%
  \def\vbname{#1}%
  \def\vbdescription{#2}%
  \begingroup%
    \vb@dospaces%
    \vb@doreturns%
    \vb@dotabs%
    \vb@dospecials%
    \vb@collect%
}

\long\def\vb@collected#1{%
    \gdef\vbexamplecode{\vb@dospaces\vb@doreturns\vb@dotabs#1}%
  \endgroup%
  \gdef\vbexecutedcode{\begingroup\vb@returnspacing\scantokens{#1\ignorespaces}\endgroup}%
  \vbtypeset%
  \vbend%
}

\def\vbend{%
  \endgroup%
}

\catcode`\@=\vbatcatcode%

\newdimen\vbtmpdimen

\def\vbtypeset{%
  \parindent=0pt
  \begin{minipage}[t]{0.75in}\tt\vbname\end{minipage}%
  \vbtmpdimen=\linewidth
  \advance\vbtmpdimen-0.75in%
  \begin{minipage}[t]{\vbtmpdimen}
    \vbdescription\medskip\par\leavevmode
    Example:
    \medskip\par\leavevmode%
    \vbexecutedcode%
    \medskip\par\leavevmode
    {\ttfamily\vbexamplecode}%
  \end{minipage}%
  \bigskip}

\begin{document}
\begin{vb}{tabular}
{This is the standard latex environment for tabular data.}
\begin{tabular}{ccc}
\hline
  col1 & col2 & col3 \\
\hline
  dat1 & dat2 & dat3 \\
  dat4 & dat5 & dat6 \\
\hline
\end{tabular}
\end{vb}

\end{document}

在此处输入图片描述

答案3

我的解决方案使用临时外部文件。以逐字模式扫描数据并保存到文件中。然后使用正常的 catcode 设置读取文件,因此打印正常输出。然后逐字扫描的数据用于逐字输出。

\documentclass{article}

\newwrite\verbfile
\def\setverb{\def\do##1{\catcode`##1=12}\dospecials}
\def\begverbA{\bgroup \setverb \obeyspaces \obeylines \begverbB}
\def\tmp#1{}\edef\bslash{\expandafter\tmp\string\\}
\edef\tmp{\def\noexpand\begverbB##1\bslash end\string{showcode\string}##2}
\tmp{\egroup\par
   %%% normal output:
   {\newlinechar=`\^^J \obeylinesJ
    \immediate\openout\verbfile=verb-tmp.tex
    \immediate\write\verbfile{#1}%
    \immediate\closeout\verbfile}
   \input verb-tmp.tex
   \par\bigskip
   %%% verbatim output:
   {\tt\def\par##1{\endgraf\ifx##1\relax\else\leavevmode\fi##1}
    \obeylines#1\relax}%
   \par\medskip
   \end{showcode}%
}
{\catcode`\^^M=\active \gdef\obeylinesJ{\catcode`\^^M\active \def^^M{^^J}}}

\newenvironment{showcode}[2]{%
   \par\medskip
   \bgroup\leftskip=5em
          \noindent\llap{\tt#1\quad}#2\par
          \bigskip\begverbA}
   \egroup

\begin{document}
\begin{showcode}{tabular}
                {This is the standard \LaTeX\ environment for tabular data.}
\begin{tabular}{ccc}
\hline
  col1 & col2 & col3 \\
\hline
  dat1 & dat2 & dat3 \\
  dat4 & dat5 & dat6 \\
\hline
\end{tabular}
\end{showcode}

\end{document}

答案4

使用破解该example包的方法。

example.sty直接制作自己的软件包而不是用这种方式混淆序言可能是个好主意。

平均能量损失

\documentclass[a4paper]{article}
\usepackage[margin=3cm]{geometry,xcolor}
\usepackage{example,booktabs}
\parindent0pt\parskip0em
\columnsep .1\textwidth  
\makeatletter
\chardef\escape=0
\chardef\open=1
\chardef\close=2
\chardef\letter=11
\chardef\other=12
\chardef\uscode=\catcode`\_   
\catcode`\_=\letter
\def\example#1#2{
\fboxsep1em
\fboxrule0em
\bigskip{\color{blue!10}\rule{\textwidth}{2.5pt}}\par\bigskip
\noindent\hangindent.2\linewidth\makebox[.2\linewidth][l]{\texttt{#1}}%
#2\par\bigskip\noindent{\color{blue!10}\rule{\textwidth}{2pt}}\par\bigskip
   \par
   \immediate\openout\example_file\example_name
   \begingroup
      \@makeother\"\let\do\@makeother \dospecials
      \obeylines \obeyspaces
      \@ignoretrue \copy_line
      }

\def\ExampleSet{%
\noindent\fcolorbox{red}{cyan!10}{
   \begin{minipage}[t]{.4\textwidth}%
      \hrule height\z@
      \def\markboth##1##2{}%
      \def\markright##1{}%
      \def\addcontentsline##1##2##3{}%
      \input \example_name
      \par
      \hrule height\z@
   \end{minipage}}}      

   \def\ExampleVerb{%
\noindent\fcolorbox{red}{green!10}{%
   \begin{minipage}[t]{.4\textwidth}%
      \hrule height\z@
      \begingroup
         \small
         \parindent\z@
         \rightskip\@flushglue
         \@makeother\"\@verbatim
         \frenchspacing \@vobeyspaces \@vobeytabs
         \input \example_name
         \endverbatim
      \endgroup
      \hrule height\z@
   \end{minipage}}}

\makeatother

\begin{document}

\begin{example}{tabular}{This is the standard \LaTeX\ environment for tabular data, with optional horizontal and vertical lines.}
%\begin{example}
\begin{tabular}{ccc}
\toprule 
col1 & col2 & col3 \\
\midrule
dat1 & dat2 & dat3 \\
dat4 & dat5 & dat6 \\
\bottomrule
\end{tabular} 
\end{example}

\begin{example}{quote}{This is a  standard \LaTeX\ environment for quotes.}
This is main text.
\begin{quote}
This is a quote
\end{quote} 
This is again the main text.
\end{example}

\end{document}

相关内容