# How insert a piece of code before and after environment?

An example, how decrease vertical space using itemize?

1. I can insert the code: \addtolength{\itemsep}{-0.5em} at begin each itemize.

2. I can define and use a new environment as:

\newenvironment{myItemize}{
\begin{itemize}
}{
\end{itemize}
}


But how can I call myItemize without replace all itemize?

I want do the same thing for equations inserting \vspace{-0.5em} beginning and ending equation environment.

• Welcome to TeX SX! You should take a look at the enumitem package to define customised lists. Apr 19 '18 at 23:51

There are a number of ways to achieve this. First, by redefining the way itemize works. For this, let's see what the itemize environment looks like (from the LaTeX kernel, latex.ltx):

\def\itemize{%
\ifnum \@itemdepth >\thr@@\@toodeep\else
\edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
\expandafter
\list
\csname\@itemitem\endcsname
{\def\makelabel##1{\hss\llap{##1}}}%
\fi}
\let\enditemize =\endlist


Note that

\begin{itemize}
\end{itemize}


is roughly translated to

\begingroup
\itemize
\enditemize
\endgroup


So, one could just copy the definition of \itemize into your preamble and change it to suit your needs. Here's an example illustrating the changes mid-document:

\documentclass{article}

\begin{document}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

% Redefine \itemize (\def will overwrite the definition)
\makeatletter
\def\itemize{%
\ifnum \@itemdepth >\thr@@\@toodeep\else
\edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
\expandafter
\list
\csname\@itemitem\endcsname
{\def\makelabel##1{\hss\llap{##1}}%
\fi}
\makeatother

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

\end{document}


A different approach would be to copy the definition of \itemize into a different macro (say \tempitemize) and then redefine \itemize to call \tempitemize, followed by your insertions:

\documentclass{article}

\begin{document}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

% Copy and redefine \itemize
\let\tempitemize\itemize % Store/copy \itemize in \tempitemize
\def\itemize{%
\tempitemize % Original definition of \itemize
}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

\end{document}


The redefinition is a little simpler, but relies on the fact that your insertions happen after the (complete) execution of \itemize.

Maybe you don't want to fiddle with things at that level. The, perhaps, you can modify \itemize using a search-and-replace feature from etoolbox's \patchcmd.

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

% Patch \itemize to insert some code
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\itemize} % <cmd>
{\fi} % <search>
{}{} % <success><failure>

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

\end{document}


The above patch would be similar to changing the original definition of \itemize to this:

\def\itemize{%
\ifnum \@itemdepth >\thr@@\@toodeep\else
\edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
\expandafter
\list
\csname\@itemitem\endcsname
{\def\makelabel##1{\hss\llap{##1}}}%
\fi}


While this is slightly different from the first and the second option, the result is the same. This solution relies on the fact that you know the structure of the command you're patching. That is, \fi has to be within \itemize in order for the patch to work, otherwise nothing happens. Moreover, the first occurrence of \fi is prepended with \addtolength{\itemsep}{-0.5em}, so you'll have issues if there are multiple \fis within \itemize (say).

Finally, you can use an extendable approach with the enumitem package:

\documentclass{article}

\usepackage{enumitem}

\begin{document}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

\begin{itemize}
\item First
\item Second
\item Third
\item Last
\end{itemize}

\end{document}


Of course, you could also specify a new value for \itemsep using the itemsep = <value> key-value approach

\setlist[itemize]{itemsep = <value>}


Note that inserting this before/after equation environments might be done in a better way by adjusting the values of

• \abovedisplayskip - length above a display equation that is preceded by a short line of text;
• \abovedisplayshortskip - length above a display equation that is preceded by a long line of text;
• \belowdisplayskip - similar to \abovedisplayskip but below the display equation; and
• \belowdisplayshortskip - similar to \abovedisplayshortskip but below the display equation.

For more on this, see

• Perfect! But and about equation environment? Should I open latex.ltx and redefine the piece of code in tex file or there is a package like enumitem? Apr 20 '18 at 16:36
• @BismarckGomes -- it is never a good idea to change an established package directly, and even worse to change a core component such as latex.ltx'. if you want something you can use more than once, you can create your own little .sty file with the replacement code, and then call it with \usepackage`. Apr 20 '18 at 16:41