我孔认为不可能有逐字内容\BODY
但至少可以使用一个主动角色吗?
我将 定义<x>
为 的快捷方式,\fbox{x}
并希望能够使用 激活它\activebox
。通常,它在正常环境中工作正常,即使我使用包装器,但如果直接在环境中激活则不行environ
。
\documentclass[ngerman]{article}
\usepackage{environ}
\begingroup
\catcode`< = \active
\gdef<#1>{
\fbox{#1}
}
\endgroup
\def\activatebox{
\catcode`<=\active
}
\NewEnviron{test}{
\begin{itemize}
\BODY
\end{itemize}
}
\newenvironment{testwrapper}[1]{
#1
\test
} {
\endtest
}
\begin{document}
Test <Box>
\begin{itemize}
\activatebox
\item Test <Box>
\end{itemize}
\begin{test}
\activatebox
\item Test <Box>
\end{test}
\begin{testwrapper}{\activatebox}
\item Test <Box>
\end{testwrapper}
\end{document}
在我的实际示例中,它当然会更复杂一些:\BODY
打印在文档中,并写入文件。我无法\lst@BeginWriteFile
在这里使用,因为有一个文件保存了多个\BODY
s。这是我的环境的原始定义:
更多背景 MWE
\documentclass{article}
\usepackage{environ,xparse, etoolbox}
\begingroup
\catcode`< = \active
\gdef<#1>{
\fbox{#1}
}
\endgroup
\def\activatebox{
\catcode`<=\active
}
\ExplSyntaxOn% ============================================================
\bool_new:N \tobi_in_group_of_boxes_bool
\bool_set_false:N \tobi_in_group_of_boxes_bool
\NewDocumentCommand { \mybox } { m } {
\fbox { #1 }
\bool_if:NF \tobi_in_group_of_boxes_bool {
\iow_shipout_x:Nx \tobi_solutions_iow {
\tl_to_str:n { \boxline }
{ \exp_not:n { \exp_not:N #1 } }
{ \exp_not:N \thepage }
}
}
}
\cs_generate_variant:Nn \iow_shipout:Nn { No }
\NewEnviron { boxgroup } {
\bool_set_true:N \tobi_in_group_of_boxes_bool
\iow_shipout_x:Nx \tobi_solutions_iow {
\tl_to_str:n { \begin { boxgroup } } { \exp_not:N \thepage }
}
\iow_shipout:No \tobi_solutions_iow { \BODY }
\iow_shipout_x:Nx \tobi_solutions_iow {
\tl_to_str:n { \end { boxgroup } }
}
\par\textsf{\color{black!35}GROUP~START}\par
\BODY
\par\textsf{\color{black!35}GROUP~END}\par
}
\iow_new:N \tobi_solutions_iow
\iow_open:Nn \tobi_solutions_iow { \c_sys_jobname_str . boxes }
\AfterEndDocument {
\iow_close:N \tobi_solutions_iow
}
\ExplSyntaxOff% ============================================================
\begin{document}
Test <Box A> \mybox{Box B}
\begin{itemize}
\activatebox
\item Test <Box C>
\end{itemize}
\begin{boxgroup}
\activatebox
Test <Box D>
\end{boxgroup}
\activatebox% must be part of the *.boxes file, thus can’t be
% outside of the environment
\begin{boxgroup}
Test <Box E>
\end{boxgroup}
\end{document}
答案1
<
在排版内容时替换一些内容。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{environ,xparse, etoolbox}
\ExplSyntaxOn
\cs_new_protected:Npn \tobi_active_less:w #1 >
{
\fbox{#1}
}
\char_set_active_eq:NN < \tobi_active_less:w
\NewDocumentCommand{\activatebox}{}
{
\char_set_catcode_active:N <
\bool_set_true:N \l_tobi_active_less_bool
}
\cs_new:Npn \tobi_perhaps_active_less:
{
\bool_if:NTF \l_tobi_active_less_bool { \tobi_active_less:w } { < }
}
\bool_new:N \l_tobi_active_less_bool
\bool_new:N \l_tobi_in_group_of_boxes_bool
\bool_set_false:N \l_tobi_in_group_of_boxes_bool
\NewDocumentCommand { \mybox } { m }
{
\fbox { #1 }
\bool_if:NF \l_tobi_in_group_of_boxes_bool
{
\iow_shipout:Nx \tobi_solutions_iow
{
\exp_not:n { \boxline{#1} } { \thepage }
}
}
}
\cs_generate_variant:Nn \iow_shipout:Nn { NV }
\NewEnviron { boxgroup }
{
\bool_set_true:N \l_tobi_in_group_of_boxes_bool
\iow_shipout:Nx \tobi_solutions_iow
{
\exp_not:N \begin{boxgroup}{\thepage}
}
\iow_shipout:NV \tobi_solutions_iow \BODY
\iow_shipout:Nn \tobi_solutions_iow
{
\end{boxgroup}
}
\par\textsf{\color{black!35}GROUP~START}\par
\tl_replace_all:Nnn \BODY { < } { \tobi_perhaps_active_less: }
\BODY
\par\textsf{\color{black!35}GROUP~END}\par
}
\iow_new:N \tobi_solutions_iow
\iow_open:Nn \tobi_solutions_iow { \c_sys_jobname_str . boxes }
\AfterEndDocument
{
\iow_close:N \tobi_solutions_iow
}
\ExplSyntaxOff
\begin{document}
Test <Box A> \mybox{Box B}
\begin{itemize}
\activatebox
\item Test <Box C>
\end{itemize}
\begin{boxgroup}
\activatebox
Test <Box D>
\end{boxgroup}
\mybox{Abc\textbf{def}}
\end{document}
这就是将要写入.boxes
文件中的内容;请注意,我更改了代码,以便\thepage
能够扩展,而您的代码却不能。
\boxline {Box B}{1}
\begin {boxgroup}{1}
\activatebox Test <Box D>
\end {boxgroup}
\boxline {Abc\textbf {def}}{1}
答案2
我可能会采取以下措施(不知道最终目标是什么):
\documentclass{article}
\usepackage{environ,xparse,etoolbox}
\ExplSyntaxOn
\cs_new_protected:Npn \tobi_frame_box:w #1 > { \fbox {#1} }
\char_set_active_eq:NN < \tobi_frame_box:w
\tl_new:N \l__tobi_tmpa_tl
\bool_new:N \l_tobi_in_group_of_boxes_bool
\bool_set_false:N \l_tobi_in_group_of_boxes_bool
\cs_new_protected:Npn \tobi_handle_boxgroup:n #1
{
\bool_set_true:N \l_tobi_in_group_of_boxes_bool
\iow_shipout_x:Nx \l_tobi_solutions_iow {
\tl_to_str:n { \begin { boxgroup } } { \exp_not:N \thepage }
}
\iow_shipout:Nn \l_tobi_solutions_iow {#1}
\iow_shipout_x:Nx \l_tobi_solutions_iow {
\tl_to_str:n { \end { boxgroup } }
}
\par\textsf{\color{black!35}GROUP~START}\par
#1
\par\textsf{\color{black!35}GROUP~END}\par
}
\cs_generate_variant:Nn \tobi_handle_boxgroup:n {V}
\NewEnviron {boxgroup} {
\tl_set_rescan:NnV \l__tobi_tmpa_tl
{ \char_set_catcode_active:N < }
\BODY
\tobi_handle_boxgroup:V \l__tobi_tmpa_tl
}
\cs_generate_variant:Nn \tl_set_rescan:Nnn {NnV}
\iow_new:N \l_tobi_solutions_iow
\iow_open:Nn \l_tobi_solutions_iow { \c_sys_jobname_str . boxes }
\AfterEndDocument {
\iow_close:N \l_tobi_solutions_iow
}
\ExplSyntaxOff
\begin{document}
\begin{boxgroup}
Test <Box D>
\end{boxgroup}
\begin{boxgroup}
Test <Box E>
\end{boxgroup}
\end{document}
test.boxes
\begin {boxgroup}{1}
Test <Box D>
\end {boxgroup}
\begin {boxgroup}{1}
Test <Box E>
\end {boxgroup}