這是問題的後續:如何在環境中排版指令及其等效文字?
顯然,它對於處理環境也很有用。我想像如下:
句法
\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 的程式碼,但我在這裡展示瞭如何使用filecontents
,etoolbox
和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
懷疑這就是OP想要的,但時不時地從(幾乎)第一原則做一些事情是很好的,並說服自己,我幾年前花在閱讀斯蒂芬·馮·貝托爾斯海姆的所有時間上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
使用包的 hack 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}