创建内容块以便在整个文档中多次引用

创建内容块以便在整个文档中多次引用

我必须创建一系列练习,所有练习都包含相同的项目:

  • 标题
  • 图像
  • 问题
  • 解决方案
  • ETC..

我想为每个练习创建一个单独的文件,然后从我的主文档中多次引用这些特定部分。我希望能够在每个项目中放入方程式、图像、tikz 等。

我考虑为每种情况创建一个包含宏定义的文件,例如:

ex1.sty

\newcommand\ex1sol{
(contents)
}
...

然后在需要的地方调用宏,但这样不起作用,我觉得无论如何都一定有更好的解决方案,因为这似乎不是一个罕见的应用程序。本质上,这意味着以不同的格式呈现相同的内容。

答案1

作为变体我的答案使用 Latex 代码块作为库 - 用于在多种表示中重复使用 Latex 元素(例如图形)我可以提供一个界面,其中具有可指定部分的文件具有模式

\filesection{⟨name of section⟩}
⟨code⟩
\endfilesection
%-------------------------------------------------------------
\filesection{⟨name of section⟩}
⟨code⟩
\endfilesection
%-------------------------------------------------------------
\filesection{⟨name of section⟩}
⟨code⟩
\endfilesection
%-------------------------------------------------------------
\endinput

使用命令输入这些文件

\inputfilesection{⟨name of file⟩}{⟨name of section⟩}

⟨代码⟩仅当 -command\inputfilesection\filesection-comand 具有相同的⟨部分名称⟩-争论。

在这种情况下⟨代码⟩将在 verbatim-catcode-régime 下读取。然后将结果传递到\scantokens重新标记和正常处理。

多个\filesections 可以有相同的⟨部分名称⟩. 在这种情况下,文件中出现的所有相关名称的文件部分均由 提供\inputfilesection

我知道的注意事项:

!!!不要在被!!!\inputfilesection调用的文件中执行操作\inputfilesection

%
% An example file with \filesection-commands:
%
%\begin{filecontents*}[overwrite]{FileWithSections.tex}
\begin{filecontents*}{FileWithSections.tex}
\filesection{First Section}
Some text in First Section Of FileWithSections.tex.
\begin{verbatim*}
Some verbatim material in 
First Section Of FileWithSections.tex.
\end{verbatim*}
\endfilesection
%-------------------------------------------------------------
\filesection{Second Section}
Some text in Second Section Of FileWithSections.tex.
\begin{verbatim*}
Some verbatim material in 
Second Section Of FileWithSections.tex.
\end{verbatim*}
\endfilesection
%-------------------------------------------------------------
\filesection{Third Section}
Some text in Third Section Of FileWithSections.tex.
\begin{verbatim*}
Some verbatim material in 
Third Section Of FileWithSections.tex.
\end{verbatim*}
\endfilesection
%-------------------------------------------------------------
\endinput
\end{filecontents*}



\documentclass{article}
\usepackage{graphicx}

\makeatletter
%///// start of code that could go into a package / .sty-file//////////////////
\@ifundefined{NewDocumentCommand}{\RequirePackage{xparse}}{}%
\NewDocumentCommand\inputfilesection{mm}{%
  \def\@stringincommand{#2}%
  \input{#1}%
}%
\newcommand\@stringincommand{}%
\newcommand\@stringinfile{}%
\NewDocumentCommand\filesection{m}{%
  \begingroup
  \let\do\@makeother\dospecials
  \do\^^I%
  \do\^^M%
  \@filesection{#1}%
}%
\begingroup
\newcommand\@filesection[2]{%
  \endgroup
  \def\@filesection##1##2#1{%
     \def\@stringinfile{##1}%
     \expandafter\endgroup
     \ifx\@stringinfile\@stringincommand\expandafter\@firstofone\else\expandafter\@gobble\fi
     {%
       \begingroup
       \newlinechar=\endlinechar
       \scantokens\expandafter\expandafter\expandafter{%
       \expandafter\expandafter\expandafter\endgroup
       \UD@RemoveLeadingNTrailingCarriageReturn{##2}#2}%
     }\ignorespaces
  }%
}%
\@firstofone{%
  \let\do\@makeother\dospecials
  \catcode`\{=1 %
  \catcode`\}=2 %
  \@filesection
}{\endfilesection}{\ignorespaces%}%
%=================================================================================
% Begin of code for removing one leading and one trailing explicit
% <carriage-return>-character-token of catcode 12(other) from _verbatimized_ 
% argument
%=================================================================================
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral\expandafter\@secondoftwo\string{\expandafter
  \@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
  \@secondoftwo\string}\expandafter\UD@stopromannumeral\@secondoftwo}{%
  \expandafter\UD@stopromannumeral\@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
\begingroup
\@makeother\^^M%
\@firstofone{%
  \endgroup%
  %%=============================================================================
  %% Check whether_verbatimized_ argument has a leading explicit 
  %% <carriage-return>-character-token of catcode 12(other):
  %%-----------------------------------------------------------------------------
  %% \UD@CheckWhetherLeadingCarriageReturn{<Argument which is to be checked>}%
  %%                                      {<Tokens to be delivered in case
  %%                                       <argument which is to be checked>'s
  %%                                       1st token is an explicit <carriage-
  %%                                       return>-character-token of 
  %%                                       catcode 12(other)>}%
  %%                                      {<Tokens to be delivered in case
  %%                                       <argument which is to be checked>'s
  %%                                       1st token is not an explicit
  %%                                       <carriage-return>-character-token of 
  %%                                       catcode 12(other)>}%
  \newcommand\UD@CheckWhetherLeadingCarriageReturn[1]{%
    \UD@@CheckWhetherLeadingCarriageReturn\UD@SelDom#1\UD@SelDom^^M\UD@@SelDom%
  }%
  \@ifdefinable\UD@@CheckWhetherLeadingCarriageReturn{%
    \long\def\UD@@CheckWhetherLeadingCarriageReturn#1\UD@SelDom^^M#2\UD@@SelDom{%
      \UD@CheckWhetherNull{#2}{\@secondoftwo}{\@firstoftwo}%
    }%
  }%
  %%-----------------------------------------------------------------------------
  %% Check whether_verbatimized_ argument having a leading explicit <carriage-
  %% return>-character-token of catcode 12(other) consists only of such tokens:
  %%-----------------------------------------------------------------------------
  %% \UD@CheckWhetherOnlyCarriageReturn{<Argument which is to be checked and 
  %%                                    which is known to have a leading explicit
  %%                                    <carriage-return>-character-token of
  %%                                    catcode 12(other)>}%
  %%                                   {<Tokens to be delivered in case <argument
  %%                                    which is to be checked> consists only
  %%                                    of explicit <carriage-return>-character-
  %%                                    tokens of catcode 12(other)>}%
  %%                                   {<Tokens to be delivered in case <argument
  %%                                    which is to be checked> does not consist
  %%                                    only of explicit <carriage-return>-
  %%                                    character-tokens of catcode 12(other)>}%
  \newcommand\UD@CheckWhetherOnlyCarriageReturn[1]{%
    \UD@CheckWhetherLeadingCarriageReturn{#1}{%
      \expandafter\UD@CheckWhetherOnlyCarriageReturn%
      \expandafter{\UD@@TrimLeadingCarriageReturn#1}%
    }{%
      \UD@CheckWhetherNull{#1}{\@firstoftwo}{\@secondoftwo}%
    }%
  }%
  %%-----------------------------------------------------------------------------
  %% Remove one leading explicit <carriage-return>-character-token of 
  %% catcode 12(other) from _verbatimized_ argument:
  %%-----------------------------------------------------------------------------
  \@ifdefinable\UD@@TrimLeadingCarriageReturn{%
    \long\def\UD@@TrimLeadingCarriageReturn^^M{}%
  }%
  %%-----------------------------------------------------------------------------
  %% Check whether_verbatimized_ argument has a trailing explicit 
  %% <carriage-return>-character-token of catcode 12(other):
  %%-----------------------------------------------------------------------------
  %% \UD@CheckWhetherTrailingCarriageReturn{<Argument which is to be checked>}%
  %%                                      {<Tokens to be delivered in case
  %%                                       <argument which is to be checked>'s
  %%                                       last token is an explicit <carriage-
  %%                                       return>-character-token of 
  %%                                       catcode 12(other)>}%
  %%                                      {<Tokens to be delivered in case
  %%                                       <argument which is to be checked>'s
  %%                                       last token is not an explicit
  %%                                       <carriage-return>-character-token of 
  %%                                       catcode 12(other)>}%
  \newcommand\UD@CheckWhetherTrailingCarriageReturn[1]{%
    \UD@@CheckWhetherTrailingCarriageReturn#1\UD@SelDom^^M\UD@SelDom\UD@@SelDom%
  }%
  \@ifdefinable\UD@@CheckWhetherTrailingCarriageReturn{%
    \long\def\UD@@CheckWhetherTrailingCarriageReturn#1^^M\UD@SelDom#2\UD@@SelDom{%
      \UD@CheckWhetherNull{#2}{\@secondoftwo}{\@firstoftwo}%
    }%
  }%
  %%-----------------------------------------------------------------------------
  %% Remove one trailing explicit <carriage-return>-character-token of 
  %% catcode 12(other) from _verbatimized_ argument:
  %%-----------------------------------------------------------------------------
  \newcommand\UD@TrimTrailingCarriageReturn[1]{%
     \UD@@TrimTrailingCarriageReturn#1\UD@SelDom%
  }%
  \@ifdefinable\UD@@TrimTrailingCarriageReturn{%
    \long\def\UD@@TrimTrailingCarriageReturn#1^^M\UD@SelDom{#1}%
  }%
  %%-----------------------------------------------------------------------------
  %% Remove one leading and one trailing explicit <carriage-return>-character-
  %% token of catcode 12(other) from _verbatimized_ argument if present.
  %% In the edge case of the _verbatimized_ argument consisting only of explicit
  %% <carriage-return>-character-tokens of catcode 12(other) remove only one of 
  %% them.
  %% Due to \romannumeral-expansion the result is delivered in 2 expansion-steps:
  %%-----------------------------------------------------------------------------
  \newcommand\UD@RemoveLeadingNTrailingCarriageReturn[1]{%
    \romannumeral%
    \UD@CheckWhetherLeadingCarriageReturn{#1}{%
      \UD@CheckWhetherTrailingCarriageReturn{#1}{%
         \UD@CheckWhetherOnlyCarriageReturn{#1}{%
           \expandafter\UD@stopromannumeral\UD@@TrimLeadingCarriageReturn#1%
         }{%
           \expandafter\expandafter\expandafter\expandafter\expandafter%
           \expandafter\expandafter\UD@stopromannumeral\expandafter%
           \UD@TrimTrailingCarriageReturn\expandafter{\UD@@TrimLeadingCarriageReturn#1}%
         }%
      }{%
        \expandafter\UD@stopromannumeral\UD@@TrimLeadingCarriageReturn#1%
      }%
    }{%
      \UD@CheckWhetherTrailingCarriageReturn{#1}{%
        \expandafter\expandafter\expandafter\UD@stopromannumeral%
        \UD@TrimTrailingCarriageReturn{#1}%
      }{\UD@stopromannumeral#1}%
    }%
  }%
}%
%================================================================================
% End of code for removing one leading and one trailing explicit
% <carriage-return>-character-token of catcode 12(other) from _verbatimized_ 
% argument
%================================================================================
%///// end of code that could go into a package / .sty-file////////////////////
\makeatother


\begin{document}

\noindent
\inputfilesection{FileWithSections.tex}{First Section}

\noindent\hrule\null

\noindent
\inputfilesection{FileWithSections.tex}{Second Section}

\noindent\hrule\null

\noindent
\inputfilesection{FileWithSections.tex}{Third Section}

\noindent\hrule\null

\noindent
\inputfilesection{FileWithSections.tex}{First Section}

\noindent\hrule\null

\noindent
\inputfilesection{FileWithSections.tex}{Second Section}

\noindent\hrule\null

\noindent
\inputfilesection{FileWithSections.tex}{Third Section}

\end{document}

在此处输入图片描述

相关内容