
나는 모두 동일한 항목을 사용하여 일련의 연습을 만들어야 합니다.
- 제목
- 영상
- 질문
- 솔루션
- 등..
각 연습에 대해 별도의 파일을 생성한 다음 기본 문서에서 이러한 특정 섹션을 여러 번 참조할 수 있도록 하고 싶습니다. 각 항목 내에 방정식, 이미지, tikz 등을 넣을 수 있기를 원합니다.
나는 각 경우에 대한 매크로 정의가 포함된 파일을 만드는 것을 고려했습니다. 예:
ex1.sty
\newcommand\ex1sol{
(contents)
}
...
그런 다음 필요할 때 매크로를 호출했지만 이것이 작동하지 않고, 이것이 드문 응용 프로그램처럼 보이지 않기 때문에 어쨌든 더 나은 솔루션이 있어야 한다고 생각합니다. 본질적으로 이는 동일한 콘텐츠를 다른 형식으로 표시하는 것을 의미합니다.
답변1
의 변형으로내 대답에게라텍스 코드 청크를 라이브러리로 사용 - 여러 표현의 그림과 같은 라텍스 요소를 재사용하기 위해지정 가능한 섹션이 있는 파일이 패턴인 인터페이스를 제공할 수 있습니다.
\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⟩}
그만큼⟨암호⟩\inputfilesection
-command와 \filesection
-comand가 모두 동일한 경우에만 파일 섹션이 실행됩니다.⟨구간명⟩-논쟁.
이 경우⟨암호⟩verbatim-catcode-regime 아래에서 읽을 수 있습니다. 그런 다음 \scantokens
재토큰화 및 일반 처리를 위해 결과가 전달됩니다 .
여러 개의 \filesection
s가 동일할 수 있음⟨구간명⟩. 이 경우 파일에서 발생하는 문제의 이름의 모든 파일 섹션은 에 의해 전달됩니다 \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}