# Is there an easy way to patch commands containing ^^J?

I'm currently looking at various questions related to multicol and try giving answers to some of them. Often enough the answer would involve patching \balance@columns or similar macros and in that case \patchcmd from etoolbox fails because there are newline characters in the macro, e.g.,

       \message{^^JColumn
\number\@tempcnta\space


Now, I can of course add the whole definition of \balance@columns to my answer, but this command has 165 lines, so I would rather like to avoid this.

Is there any easy way to patch nevertheless (or get rid of the ^^J from the outside) that I'm missing?

Not sure why ^^J is a problem, isn't it only special as \newlinechar when stuff is being used in \write ?

\documentclass{article}

\def\zz{aaa^^Jbbb \foo ccc}

\def\patchzz#1\foo#2\zzz{\def\zz{#1\bar#2}}

\show\zz
\expandafter\patchzz\zz\zzz

\show\zz

\stop


ah if \patchcmd uses \scantokens that is a write, so you probably just want to locally reset \newlinechar.

An example with etoolbox (From Joseph:-)

\documentclass{article}
\usepackage{etoolbox}
\tracingpatches
\usepackage{multicol}
\makeatletter
\show\balance@columns
\edef\reset@newlinechar{\newlinechar\the\newlinechar}
\newlinechar-1 %
\patchcmd{\balance@columns}
{\ifshr@nking \vfill \fi}
{\CHANGED}
{}
{}
\show\balance@columns
\reset@newlinechar

• Cool; I know it should have been easy, so that is how you gain reputation, stealing from @JosephWright :-) – Frank Mittelbach Sep 1 '13 at 17:11

You can use regexpatch:

\documentclass{article}
\usepackage{regexpatch}
\tracingxpatches
\usepackage{multicol}
\makeatletter
\show\balance@columns
\xpatchcmd{\balance@columns}
{\ifshr@nking \vfill \fi}
{\CHANGED}
{}
{}
\show\balance@columns