# How can I modify environments on beamer without knowing their original definition

My question is similar to that. I've also tried this, but didn't work for me.

Here is a MWE:

\documentclass{beamer}
\usepackage{ragged2e}
\usepackage{lipsum}
\begin{document}
\footnotesize
\begin{frame}\justifying
\lipsum[11]
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{itemize}\justifying
\item \lipsum[11]
\end{itemize}
\end{column}
\begin{column}{0.5\columnwidth}\justifying
\lipsum[11]
\end{column}
\end{columns}
\end{frame}
\end{document}


I want to modify the frame, itemize and column environments to append the \justifying command to they.

I don't want to create new environments.

Any suggestions welcome!

• \xapptocmd from xpatch package but you should really know what you do here. I don't recommend it applying it without knowing the definition – user31729 Oct 5 '15 at 21:25
• @ChristianHupfer your sugestion works well for frame and itemize, but not for column, unfortunately. – paulequilibrio Oct 5 '15 at 21:50
• Yes, since column has an argument (or even an optional argument) and here the \xapptocmd breaks -- you've to know how it's defined – user31729 Oct 5 '15 at 21:53
• Why do you want to do it without knowing their definitions particularly? If I give you a package which makes the changes, will that do if you don't have to read the source of my package? (I.e. you can choose ignorance.) – cfr Oct 5 '15 at 22:06
• @paulequilibrio: See my undeleted answer as another suggestion – user31729 Oct 5 '15 at 22:14

The \xapptocmd appends code to other commands (under certain circumstances), as well as to the environment starter commands \frame etc.

However, in order to make this code really working, it's better to know how the definitions are. \xapptocmd works for \begin{frame}, but not for \begin{frame}[options]{Foo} etc. and it does not work for \begin{column} due to the optional arguments.

In order to grab this, I've decided to use xparse and \RenewDocumentEnvironment, injecting \justifying or use \AtBeginEnvironment from etoolbox

\documentclass{beamer}
\usepackage{ragged2e}
\usepackage{lipsum}
\usepackage{xpatch}
\usepackage{xparse}
\usepackage{letltxmacro}

\xapptocmd{\frame}{\justifying}{\typeout{Success}}{\typeout{Failure}}
\xapptocmd{\itemize}{\justifying}{\typeout{Success}}{\typeout{Failure}} % Useful???

\LetLtxMacro\origcolumn\column
\LetLtxMacro\origcolumnend\endcolumn

\RenewDocumentEnvironment{column}{O{}g}{%
\IfValueTF{#2}{%
\origcolumn[#1]{#2}%
}{%
\origcolumn[#1]%
}%
\justifying%
}{%
\origcolumnend%
}

\begin{document}
\footnotesize
\begin{frame}%\justifying
\lipsum[11]
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{itemize}
\item \lipsum[11]
\end{itemize}
\end{column}
\begin{column}{0.5\columnwidth}
\lipsum[11]
\end{column}
\end{columns}
\end{frame}
\end{document}


Here's a LuaLaTeX-based solution, which defines a Lua function that serves to append \justifying to all instances of \begin{frame}, \begin{itemize}, and \begin{column}{...}, where ... is arbitrary (but probably something like 0.5\textwidth). The function is run by the process_input_buffer callback, which operates at a very early stage of processing, specifically, before any "ordinary" processing by TeX takes place.

An advantage of this approach is that the environments frame, itemize, and column are not actually modified in any way. Thus, it's also not necessary to know how these environments are defined in the first place.

% !TEX TS-program = lualatex
\documentclass{beamer}
\usepackage{ragged2e} % for "\justifying" macro
\usepackage{lipsum}

\usepackage{luacode}
\begin{luacode}
line = string.gsub ( line, "\\begin{frame}",      "%0\\justifying" )
line = string.gsub ( line, "\\begin{itemize}",    "%0\\justifying" )
line = string.gsub ( line, "\\begin{column}{.-}", "%0\\justifying" )
return line
end
\end{luacode}

\begin{document}
\footnotesize
\begin{frame}
\lipsum[11]
\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{itemize}
\item \lipsum[11]
\end{itemize}
\end{column}
\begin{column}{0.5\columnwidth}
\lipsum[11]
\end{column}
\end{columns}
\end{frame}
\end{document}

• Yes, you're right: I just overlooked the \justifying there. It would probably not work for \begin{frame}{foo} etc. without really using \renewcommand and \LetLtxMacro – user31729 Oct 5 '15 at 21:44
• @Mico your answer works well, but I prefer don't use lualatex. Any other sugestion? – paulequilibrio Oct 5 '15 at 21:56
• @paulequilibrio - I'm afraid I don't have another suggestion. Incidentally, why do you prefer not to use lualatex? – Mico Oct 5 '15 at 21:59
• Because this is part of a shared work that is already too complicated and I wanted to keep it as simple as possible. – paulequilibrio Oct 5 '15 at 22:08
• @paulequilibrio - I'm also in favor of keeping things as simple as possible. :-) Lua(La)TeX may indeed still seem new -- and maybe even foreboding -- to some. However, as LuaTeX is a superset of pdfTeX, virtually all tex files that compile correctly under pdfLaTeX will compile equally well under LuaLaTeX -- subject to a few, very minor tweaks. For sure, if your co-authors happen to use, for instance, badly out of date TeX distributions and if they can't be bothered to upgrade to the latest version, asking them to switch from pdflatex to lualatex may involve more effort than they can handle... – Mico Oct 5 '15 at 22:15

If your purpose is to have everything justified, redefine \raggedright to do nothing, keeping a copy of it just in case of need.

\documentclass{beamer}
\usepackage{ragged2e}
\usepackage{lipsum}

\let\latexraggedright\raggedright % just in case
\let\raggedright\relax
\justifying % because beamer.cls issues \raggedright

\begin{document}
\footnotesize
\begin{frame}

\lipsum[11]

\begin{columns}
\begin{column}{0.5\columnwidth}

\begin{itemize}
\item \lipsum[11]
\end{itemize}

\end{column}
\begin{column}{0.5\columnwidth}
\lipsum[11]
\end{column}

\end{columns}
\end{frame}

\end{document}


I don't find it a good idea, personally.