用于包装图形或表格的模板环境

用于包装图形或表格的模板环境

我需要生成多个\newenvironments,每个 s 对应我的\newtheorem以及 exsheets questions。但是,我想描述的问题并不特定于 exsheets。我的每个新环境都必须能够环绕一个图形。我已经成功地将question环境包裹如下 -

% https://tex.stackexchange.com/a/387448/26682
\newsavebox{\wrapquestionfigure}
\newenvironment{wrapquestion}[2]{ % #1 = points, #2 = figure, body = text of question
    \savebox{\wrapquestionfigure}{#2}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \question{#1}%
}{
    \endquestion%
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

它运行完美。现在我希望对包装 s 做同样的事情 \newtheorem。我的想法是创建一个模板\newenvironment,我将向其传递环境名称X以及参数。如果(最多一个)参数<arg>为空,它将调用\X,否则\X{<arg>}

这是代码--

% new environment based on the above -- to wrap a figure or table around evironment X
\newenvironment{wrapfiginenv}[3]{ 
    % #1 = environment X (question/thm/fact/ex/exc/exercise) 
    % #2 = arguments of that particular environment X (empty if none), 
    % #3 = figure or table 
    % body = body of environment X
    \savebox{\wrapquestionfigure}{#3}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \ifthenelse{ \euqal{#2}{} } {
        \csname #1\endcsname
    } {
        \csname #1\leftbracechar#2\rightbracechar\endcsname
    }
}{
    \def\tmpcmd{end}
    \expandafter\csname\expandafter\tmpcmd{}#1\endcsname
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

无论我尝试什么排列组合

\def\tmpcmd{end}
\expandafter\csname\expandafter\tmpcmd{}#1\endcsname

不起作用。我哪里做错了?

这是一个 MWE——

\documentclass{article}
\usepackage{etoolbox,amsthm,adjustbox,tikz,ifthen} 
\usepackage[headings=runin]{exsheets} 

% new commands for wrapping questions
% https://tex.stackexchange.com/a/387448/26682
\newsavebox{\wrapquestionfigure}

\newenvironment{wrapquestion}[2]{ % #1 = points, #2 = figure, body = text of question
    \savebox{\wrapquestionfigure}{#2}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \question{#1}%
}{
    \endquestion%
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

% new environment based on the above -- to wrap a figure or table around evironment X
\newenvironment{wrapfiginenv}[3]{ 
    % #1 = environment X (question/thm/fact/ex/exc/exercise) 
    % #2 = arguments of that particular environment X (empty if none), 
    % #3 = figure or table 
    % body = body of environment X
    \savebox{\wrapquestionfigure}{#3}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \ifthenelse{ \euqal{#2}{} } {
        \csname #1\endcsname
    } {
        \csname #1\leftbracechar#2\rightbracechar\endcsname
    }
}{
    %\def\tmpcmd{end}
    %\expandafter\csname\expandafter\tmpcmd{}#1\endcsname
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

\newtheorem{thm}{Theorem}[section]
\newtheorem{fact}[thm]{Fact} % there are several more such environments

\begin{document}
    \begin{fact}
        \adjustbox{valign=t}{\begin{minipage}[t]{.6\textwidth}
                Using the adjoining table and we can compute cross products of any vector as demonstrated in the classroom.
        \end{minipage}}%
        \hfill%
        \adjustbox{valign=t}{\begin{minipage}[t]{.3\textwidth}
                \begin{center}
                    \begin{tabular}{|cc|}
                        $\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
                        $\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
                        $\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$ \\
                    \end{tabular}
                \end{center}
        \end{minipage}}
    \end{fact}

    \begin{wrapquestion}{1}{\begin{tikzpicture}
            \draw[->] (0,0) -- (0,1.5) node[above] {$a (ms^2)$};
            \draw[->] (-.3,0) -- (2,0) node[right] {$t (s)$};
            \end{tikzpicture}}
        If  $\vec{C}=\alpha\vec{A}+\beta\vec{B}$  then $\vec{A}\cdot(\vec{B}\times\vec{C})+ \vec{B}\cdot(\vec{C}\times\vec{A}) + \vec{C}\cdot(\vec{A}\times\vec{B})$
    \end{wrapquestion}

    % the idea is to make the following work just like the question above.
    %\begin{wrapfiginenv}{fact}{}{\begin{tabular}{|cc|}%
    %               $\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
    %               $\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
    %               $\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$ \\
    %           \end{tabular}}
    %   Using the adjoining table and  we can compute cross products of any vector as demonstrated in the classroom.
    %\end{wrapfiginenv}
\end{document}

答案1

\def\tmpcmd{end}
\expandafter\csname\expandafter\tmpcmd{}#1\endcsname

\expandafters的序列会扩展,{ 但是这是一个不可扩展的标记,因此它不会扩展。

所以这和

 \csname end{#1}\endcsname

它将生成一个名称中带有的 csname {,它几乎肯定不会被定义,因此它将充当\relax

你大概想要

 \csname end#1\endcsname

\endproof如果#1是,它将执行,proof 或者更简单,你可以使用

 \end{#1}

并使用\begin{#1}启动环境。


现在提供了 MWE,如果你取消注释第二个环境,你会得到

! Undefined control sequence.
<inserted text>  \euqal 

大概\equal是故意的。然后按照上述方法修复 csname 用法会导致

\documentclass{article}
\usepackage{etoolbox,amsthm,adjustbox,tikz,ifthen} 
\usepackage[headings=runin]{exsheets} 

% new commands for wrapping questions
% https://tex.stackexchange.com/a/387448/26682
\newsavebox{\wrapquestionfigure}

\newenvironment{wrapquestion}[2]{ % #1 = points, #2 = figure, body = text of question
    \savebox{\wrapquestionfigure}{#2}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \question{#1}%
}{
    \endquestion%
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

% new environment based on the above -- to wrap a figure or table around evironment X
\newenvironment{wrapfiginenv}[3]{%%%%<
    % #1 = environment X (question/thm/fact/ex/exc/exercise) 
    % #2 = arguments of that particular environment X (empty if none), 
    % #3 = figure or table 
    % body = body of environment X
    \savebox{\wrapquestionfigure}{#3}%
    \noindent\minipage[t]{\textwidth}%
    \rightskip=\dimexpr \wd\wrapquestionfigure+\columnsep\relax%
    \def\thisname{#1}%
    \ifthenelse{ \equal{#2}{} } {%%%%<
        \csname #1\endcsname
    } {%%%%<
        \csname #1\endcsname{#2}%%%%<
    }%%%%<
}{%%%%<
    \csname end\thisname\endcsname
    \endminipage\llap{\raisebox{\dimexpr-\dp\strutbox-\ht\wrapquestionfigure}{\usebox{\wrapquestionfigure}}}%%
}

\newtheorem{thm}{Theorem}[section]
\newtheorem{fact}[thm]{Fact} % there are several more such environments

\begin{document}
    \begin{fact}
        \adjustbox{valign=t}{\begin{minipage}[t]{.6\textwidth}
                Using the adjoining table and we can compute cross products of any vector as demonstrated in the classroom.
        \end{minipage}}%
        \hfill%
        \adjustbox{valign=t}{\begin{minipage}[t]{.3\textwidth}
                \begin{center}
                    \begin{tabular}{|cc|}
                        $\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
                        $\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
                        $\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$ \\
                    \end{tabular}
                \end{center}
        \end{minipage}}
    \end{fact}

    \begin{wrapquestion}{1}{\begin{tikzpicture}
            \draw[->] (0,0) -- (0,1.5) node[above] {$a (ms^2)$};
            \draw[->] (-.3,0) -- (2,0) node[right] {$t (s)$};
            \end{tikzpicture}}
        If  $\vec{C}=\alpha\vec{A}+\beta\vec{B}$  then $\vec{A}\cdot(\vec{B}\times\vec{C})+ \vec{B}\cdot(\vec{C}\times\vec{A}) + \vec{C}\cdot(\vec{A}\times\vec{B})$
    \end{wrapquestion}

    % the idea is to make the following work just like the question above.
    \begin{wrapfiginenv}{fact}{}{\begin{tabular}{|cc|}%
                   $\hat{i}\times\hat{j}=\hat{k}$ & $\hat{j}\times\hat{i}=-\hat{k}$ \\
                   $\hat{j}\times\hat{k}=\hat{i}$ & $\hat{k}\times\hat{j}=-\hat{i}$ \\
                   $\hat{k}\times\hat{i}=\hat{j}$ & $\hat{i}\times\hat{k}=-\hat{j}$ \\
               \end{tabular}}
       Using the adjoining table and  we can compute cross products of any vector as demonstrated in the classroom.
    \end{wrapfiginenv}
\end{document}

相关内容