有没有办法让一个变量返回我处于多列环境中的哪一列?谢谢!
答案1
开箱即multicol
用支持找出您是否处于第一列、最后一列或中间列中的某个点(假设您有超过 2 列)。这是使用\docolaction
文档中描述的(需要通过选项明确启用)来完成的。如果您需要更多详细信息,那么上面建议的链接可能会提供这些信息,但默认情况下multicol
会区分这 3 种情况。
答案2
以下以不可扩展的方式生成当前列。它至少需要运行 2 次 LaTeX。
\documentclass[]{article}
\usepackage[left=2mm,right=2mm]{geometry}
\usepackage[colaction]{multicol}
\usepackage{etoolbox}
\makeatletter
\newcounter{nexprex@col@count}
\newcounter{nexprex@current@column@call}
\def\nexprex@patch@last
{%
\stepcounter{nexprex@col@count}%
\protected@write\@auxout{}
{%
\string\def\string\nexprex@cur@col{\arabic{nexprex@col@count}}%
}%
}
\def\nexprex@patch@else
{%
\ifmc@firstcol
\setcounter{nexprex@col@count}{0}%
\fi
\nexprex@patch@last
}
\def\nexprex@patch@error
{%
\GenericError{}
{Patching of \string\mc@col@status@write\space failed}
{%
Make sure `colaction` was set as an option for `multicol`.%
\MessageBreak
Else you're screwed, don't use the code provided here.\MessageBreak%
}
{No further help available.}%
}
\pretocmd\mc@lastcol@status@write{\nexprex@patch@last}{}{\nexprex@patch@error}
\pretocmd\mc@col@status@write{\nexprex@patch@else}{}{\nexprex@patch@error}
\newcommand\currentcolumn
{%
\stepcounter{nexprex@current@column@call}%
\protected@write\@auxout{}
{%
\string\expandafter
\string\global
\string\expandafter
\string\let
\string\csname\space
nexprex@current@column@\arabic{nexprex@current@column@call}%
\string\endcsname
\string\nexprex@cur@col
}%
\ifcsname
nexprex@current@column@\arabic{nexprex@current@column@call}\endcsname
\csname
nexprex@current@column@\arabic{nexprex@current@column@call}\endcsname
\fi
}
\makeatother
\begin{document}
\begin{multicols}{5}
\noindent
This is: \currentcolumn
\columnbreak\\
This is: \currentcolumn
\columnbreak\\
This is: \currentcolumn
\columnbreak\\
This is: \currentcolumn
\columnbreak\\
This is: \currentcolumn
\end{multicols}
\end{document}