2

it there a way to have a variable that returns in which column of my multicols environment am I in? Thanks!

3
5

Out of the box multicol supports finding out if you are at a certain point in the first, the last or one of the middle columns (assuming you have more than 2 columns). That is done using \docolaction which is described in the documentation (and needs to be explicitly enabled via an option). If you need more detail then perhaps the link suggested above provides that, but by default multicol distinguishes ones the 3 cases.

5

The following yields the current column unexpandably. It needs at least 2 runs of 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}
2
  • Well done! Why we need at least 2 runs of LaTeX? Why should this happen?
    – manooooh
    Jan 22 '19 at 16:13
  • 1
    @manooooh because the code I inject into \mc@lastcol@status@write and \mc@col@status@write is executed at ship out time and the counter isn't yet increased. Therefore we move the definitions of the current columns into the aux file.
    – Skillmon
    Jan 22 '19 at 17:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.