检测多列环境并结束它

检测多列环境并结束它

我创建了一个命令,用于.tex在文档中的特定页码处输入文件(在书的中间页)。除了一件事外,它工作正常:

  • multicol如果输入文件周围有环境.tex,则该.tex文件也将位于相同的多列环境中。我希望它检测是否使用了多列环境。如果是,则应结束并随后重新启动。

以下是 MWE:

\documentclass[fontsize=13pt,a4paper,openany,parskip=half]{scrbook}

\RequirePackage[utf8]{inputenc}
\RequirePackage{fancyhdr}
\RequirePackage{multicol}
\RequirePackage{lipsum}
\RequirePackage{tikz}
%
\RequirePackage{refcount,lastpage}
\RequirePackage{atbegshi}

\pagestyle{fancy}
% Some other commands regarding pagestyle
% is defined here.
\renewcommand{\headrulewidth}{2pt}

\ExplSyntaxOn

\tl_new:N \g_midtsidegraf_page_total
\tl_gset:Nn \g_midtsidegraf_page_total
{ \the\numexpr\getpagerefnumber{LastPage}+\int_mod:nn { 4-\int_mod:nn { \getpagerefnumber{LastPage} } {4} } {4}\relax }


\tl_new:N \g_midtsidegraf_page_after_mid
\tl_gset:Nn \g_midtsidegraf_page_after_mid
{ \the\numexpr\g_midtsidegraf_page_total / 2 + 2 \relax}


\tl_new:N \g_midtsidegraf_pagenumber_default_value
\tl_set:Nn \g_midtsidegraf_pagenumber_default_value {3}

\tl_new:N \g_midtsidegraf_pagenumber_actual_value
\tl_new:N \l__midtsidegraf_page_before_mid

\NewDocumentCommand{\midtsidegraf}{o m}
{   \IfValueTF {#1}
    { \tl_gset:Nn \g_midtsidegraf_pagenumber_actual_value {#1} }
    { \tl_gset:Nn \g_midtsidegraf_pagenumber_actual_value { \g_midtsidegraf_pagenumber_default_value } }
    \tl_set:Nn \l__midtsidegraf_page_before_mid
    { \the\numexpr\g_midtsidegraf_page_after_mid - \g_midtsidegraf_pagenumber_actual_value - 1\relax }
    \int_compare:nNnTF { \l__midtsidegraf_page_before_mid } = { \thepage }
    {\clearpage \input{#2} \clearpage}
    {}
    \int_compare:nNnTF { \the\numexpr\l__midtsidegraf_page_before_mid+\g_midtsidegraf_pagenumber_actual_value -2\relax } = { \thepage }
    {\thispagestyle{empty}}
    {}
    \int_compare:nNnTF { \the\numexpr\l__midtsidegraf_page_before_mid+\g_midtsidegraf_pagenumber_actual_value-1\relax } = { \thepage }
    {\thispagestyle{empty}}
    {}
}%
\ExplSyntaxOff

\newcommand\blankpage[1][1]{%
    \foreach \x in {1,...,#1}{%
    \clearpage
    \null
    \thispagestyle{fancy}%
    \clearpage}
}%

\begin{filecontents}[overwrite]{sometexfile.tex}

\section{Hello}

\begin{multicols}{2}
\lipsum[1]
\end{multicols}

\newpage

\tikz[remember picture,overlay] 
\tikzset{shift={(current page.center)},xshift=0.5\paperwidth}
\node[opacity=1,inner sep=0pt]{\includegraphics[height=\paperheight]{example-image-a}};

\newpage

\tikz[remember picture,overlay] 
\tikzset{shift={(current page.center)},xshift=-0.5\paperwidth}
\node[opacity=1,inner sep=0pt]{\includegraphics[height=\paperheight]{example-image-a}};

\end{filecontents}

\begin{filecontents}[overwrite]{texfileno2.tex}

\begin{multicols}{2}

\lipsum[1-10]

\end{multicols}

\end{filecontents}

\AtBeginShipout{%
    \midtsidegraf[3]{sometexfile.tex}
}

\begin{document}

\ExplSyntaxOn
\begin{tabular}{l r}
    Last~Page: & \getpagerefnumber{LastPage} \\
    Rounded~total: & \g_midtsidegraf_page_total \\
    Page~after~input~command: & \g_midtsidegraf_page_after_mid \\
\end{tabular}
\ExplSyntaxOff
\newpage

\blankpage[5]

\input{texfileno2}

\end{document}

我怎样才能以某种方式检测某个multicol环境是否正在被使用,如果正在被使用则结束它?

相关代码位是(第42行):

{\clearpage \input{#2} \clearpage}

我想切换到类似这样的内容:

{\clearpage 
\if <multicols>: 
     <save column number as n>
     \end{multicols}
\fi 
\input{#2} \clearpage 
\if <multicols>:
     \begin{multicols}{n}
\fi
}

唯一的问题是:我该怎么做\if <multicols><save column number as n>


编辑: 我试过:

\makeatletter
\newif\if@multicolsarticle
\makeatother
\ExplSyntaxOn
...
\tl_new:N \g_midtsidegraf_column_number
...
    {
    \ifnum\numexpr\col@number>\@ne
        \@multicolsarticletrue
        \tl_gset:Nn \g_midtsidegraf_column_number { \col@number }
        \end{multicols}
    \fi
    \clearpage \input{#2} \clearpage
    \if@multicolsarticle
        \begin{multicols}{\g_midtsidegraf_column_number}
    \fi
    }

但它没有起作用。

相关内容