I would like to use a \newenvironment
in my .tex file. It should use a serif font and should be left aligned.
Can someone show me the correct syntax?
I would like to use a \newenvironment
in my .tex file. It should use a serif font and should be left aligned.
Can someone show me the correct syntax?
Here are three ways of defining environments that use serif fonts (\rmfamily
here in short) and aligning them to the left. The 3rd way with \NewEnviron
uses \sffamily
and alignment to the right in order to show the differences.
The \vskip\baselineskip
is not necessary, just for visually separating them.
The regular way is \newenvironment
, having
\newenvironment{environmentname}[number of args][value of optional argument]{begin code}{end code}
-- the example here does not have arguments, so []
can be omitted there. Please note, that the arguments can not be accessed in the {end code}
section.
With \NewDocumentEnvironment
the syntax is 'similar':
\NewDocumentEnvironment{environmentname}{argument specifications}{begin code}{end code}
-- here, the arguments can be used in the end code section.
\NewEnviron
from environ
package is different in some aspects:
\NewEnviron{environment name}[number of arguments][opt argument value]{environment start code\BODY}[environment end code}
.
You need the \BODY
macro in order to display the environment content -- this is collected first in \BODY
. The end code is optional here.
\documentclass{article}
\usepackage{blindtext}
\usepackage{environ}
\usepackage{xparse}
\newenvironment{foo}{\rmfamily\flushleft}{\endflushleft}
\NewDocumentEnvironment{foobar}{}{\vskip\baselineskip\rmfamily\flushleft}{\endflushleft}
\NewEnviron{foobarbar}{\vskip\baselineskip\sffamily\flushright\BODY}[\endflushright]
\begin{document}
\begin{foo}
\blindtext
\end{foo}
\begin{foobar}
\blindtext
\end{foobar}
\begin{foobarbar}
\blindtext
\end{foobarbar}
\end{document}
Update with some font package loading
Just for example, I am using the bookman
package for the Bookman
font, which is a roman (serif) font by default -- there is no need to change \rmfamily
then:
\documentclass{article}
\usepackage{blindtext}
\usepackage{environ}
\usepackage{xparse}
\usepackage{bookman}
\newenvironment{foo}{\rmfamily\flushleft}{\endflushleft}
\NewDocumentEnvironment{foobar}{}{\vskip\baselineskip\rmfamily\flushleft}{\endflushleft}
\NewEnviron{foobarbar}{\vskip\baselineskip\sffamily\flushright\BODY}[\endflushright]
\begin{document}
\begin{foo}
\blindtext
\end{foo}
\begin{foobar}
\blindtext
\end{foobar}
\begin{foobarbar}
\blindtext
\end{foobarbar}
\end{document}
\rmfamily
accordingly
– user31729
Feb 28 '18 at 14:15
\begin{flushleft}...\end{flushleft}
? – David Carlisle Feb 28 '18 at 10:11\newenvironment{foo}{\rmfamily}{}
should be enough – user31729 Feb 28 '18 at 10:11\newenvironment{<environment-name>}{<code-at-begin>}{<code-at-end>}
. If you want to have arguments, you can use\newenvironment{<name>}[<arg-count>]
and then the rest (similar to\newcommand
). The arguments are only available in the begin-part. – Skillmon Feb 28 '18 at 10:11