How to use \scalebox around my environment?

I tried putting scalebox{.2} while defining my new environment, but it is not being applied:

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage[brazil]{babel}
\usepackage[showframe,pass]{geometry}

\usepackage{graphicx}

\makeatletter

\newenvironment{myenvironment}[0]
{scalebox{.2}\{myenvironment start\par\verbatim}
{\endverbatim\endgraf myenvironment end\}}

\makeatother

\begin{document}

\begin{myenvironment}
test
\end{myenvironment}

\end{document}


How to use \scalebox around my environment?

• – user
Aug 21, 2017 at 15:46

Two ways to do this with the verbatimbox package. Note that approach two requires a scalable font, which is why I added the lmodern package.

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

\usepackage[brazil]{babel}
\usepackage[showframe,pass]{geometry}

\usepackage{graphicx,verbatimbox}

\begin{document}

\begin{verbbox}
test
of my
\%\@# verbatim stuff
\end{verbbox}
\noindent\scalebox{.2}{\theverbbox}

versus normal size

\begin{verbnobox}[\fontsize{2pt}{2.5pt}\selectfont]
test
of my
\%\@# verbatim stuff
\end{verbnobox}

Back to normal
\end{document}


• Thanks, but when I use the font size directive I got these warnings depending on the font size: LaTeX Font Warning: Font shape T1/cmtt/m/n' in size <2> not available(Font) size <5> substituted on input line 94. However while using the scalebox it does not show these things.
– user
Aug 21, 2017 at 14:34
• @user Note that I added \usepackage{lmodern} to the preamble. Aug 21, 2017 at 14:58
• Thanks! I added it to my preamble and fixed the warnings.
– user
Aug 21, 2017 at 15:08
• Is there any way to keep blank lines in \verbbox or \verbnobox ? Sep 7, 2021 at 3:07
• Thank you Steven. It works like a charm. Sep 7, 2021 at 11:25

Or use the environ package.

\documentclass{article}

\usepackage{graphicx}
\usepackage{environ}

\NewEnviron{test}{%
\scalebox{5}{\BODY}}

\begin{document}
This is nuts
\begin{test}
This is nuts
\end{test}

\end{document}

• While the OP's question did not use the words, his/her MWE indicated verbatim content. Aug 21, 2017 at 14:33
– JPi
Aug 21, 2017 at 14:35
• Well, this works with not font size warnings, but I could not put the verbatim on it. I tried: \NewEnviron{myenvironment}{\scalebox{.2}{\par\verbatim \BODY \endverbatim\endgraf}}
– user
Aug 21, 2017 at 14:37
• Ha, just came across this question: tex.stackexchange.com/questions/51239/… Seems to answer yours.
– JPi
Aug 21, 2017 at 14:39

Quite similar to \scalebox in column specification operator >{...}, however verbatim also cannot be put inside hbox so you need the primitive setbox as suggested in https://tex.stackexchange.com/a/41707/250119:

%! TEX program = lualatex
\documentclass[10pt,a5paper,twoside]{memoir}
\usepackage{graphicx}
\newbox\mybox
\makeatletter
\ExplSyntaxOn
\newenvironment{myenvironment}[0]
{
\setbox\mybox\vbox\bgroup
myenvironment~start
\par
\verbatim}
{
\endverbatim
\endgraf myenvironment~end
\egroup
\scalebox{.2}{\box\mybox}}
\ExplSyntaxOff

\makeatother

\begin{document}
\begin{myenvironment}
test
\end{myenvironment}
\end{document}


I prefer \ExplSyntaxOn, but you can also % every lines.

• Using minipage doesn't seem to work. Jun 26, 2022 at 12:47

If you want verbatim mode, you can use fancyvrb

\documentclass[10pt,a5paper,twoside]{memoir}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage{fancyvrb}
\usepackage{graphicx}

\usepackage[showframe,pass]{geometry}

\makeatletter
\newsavebox{\mybox}
\newenvironment{myenvironment}
{\VerbatimEnvironment\begin{lrbox}{\mybox}\begin{BVerbatim}}
{%
\end{BVerbatim}\end{lrbox}%
\par\smallskip\hrule\smallskip % or whatever you want at the top
\noindent\scalebox{.2}{\usebox{\mybox}}%
\par\smallskip\hrule\smallskip % or whatever you want at the bottom
\@endpetrue
}
\makeatother

\begin{document}

Some text before the scaled verbatim
some text before the scaled verbatim
some text before the scaled verbatim
\begin{myenvironment}
test
\end{myenvironment}
Some text after the scaled verbatim
some text after the scaled verbatim
some text after the scaled verbatim

\end{document}
`