1

I want to define an environment with something like a style. That is, when putting any paragraph in that environment, it applies various formats.

For example:

  • I want the text to appear in a certain font
  • that it has a specific color, defined within the environment and if possible using a command where I define the color
  • appear in large size
  • appear in bold
  • that is not indented
  • to add an additional space with respect to the next paragraph
  • etc.

I've seen some answers here, and I've written this code:

\newenvironment{redtitle}{%
    %\bgroup
    \par
    \fontfamily{ptm}\selectfont \noindent \textcolor{red} \textbf
}
{%
    \par %\egroup
}

This is the snippet where I try to apply it:

\begin{redtitle}
    Some nifty code and stuffz
\end{redtitle}

I'm getting this error:

! Argument of \textbf has an extra }. \par l.230 \begin{redtitle} I've run across a }' that doesn't seem to match anything. For example, \def\a#1{...}' and \a}' would produce this error. If you simply proceed now, the \par' that I've just inserted will cause me to report a runaway argument that might be the root of the problem. But if your }' was spurious, just type 2' and it will go away.

Runaway argument? ! Paragraph ended before \textbf was complete. \par l.230 \begin{redtitle} I suspect you've forgotten a `}', causing me to apply this control sequence to too much text. How can we recover? My plan is to forget the whole thing and hope for the best.

Then, how I can achieve this?

I'm new to LaTeX, so if you see any bad practices or is there a better way to do this, I appreciate your feedback.

1
  • What if you replace \textbf with \bfseries? – Bernard Jan 22 at 15:31
1

You are making a couple of errors there. Mainly you are confusing macros which take argument(s) (like \textcolor and \textbf), and macros which don't (in this case the switches \color and \bfseries).

\textcolor expects two arguments (well, it's a wee bit more complicated than that but let us skirt this issue): the colour and the text to be set in that colour, so usually you have e.g.

\textcolor{red}{text which will be printed in red}

When you write

\textcolor{red} \textbf

in TeX's eyes that's pretty much the same as if you had written

\textcolor{red}{\textbf}

but \textbf is by itself a macro which expects an argument, and things will go awfully wrong (as you have experienced).

Long story short: use switches: \bfseries is a macro telling "make text from here on boldface". You need no braces there: \bfseries{...} is useless. Since environments always form a group you need no explicit \bgroup and \egroup in the environment code.

\newenvironment{redtitle}{%
    \par
    \fontfamily{ptm}\selectfont
    \noindent
    \color{red}%
    \bfseries
    \ignorespaces
}
{%
 \par
}
1

Here is an example you can customize. I have defined two environemnts. The first one is similar to yours; the second one is a list environment, which is probably more suited to your needs. The style of the paragraph is defined in the \myenvstyle macro:

\newcommand{\myenvstyle}{%
  \fontfamily{Cochineal-LF}\selectfont\color{mycolor}\Large}

while the color is defined via \definecolor (this is just one of the many ways of defining a new color with the xcolor package):

\definecolor{mycolor}{rgb}{1,0,1}

Here is a minimal working example:

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}

\definecolor{mycolor}{rgb}{1,0,1}
\newcommand{\myenvstyle}{%
  \fontfamily{Cochineal-LF}\selectfont\color{mycolor}\Large}

\newenvironment{myenv}
  {\par\vspace{\baselineskip}%
   \myenvstyle\noindent\ignorespaces}
  {\par\vspace{\baselineskip}}

\newenvironment{mylistenv}
  {\list{}{\listparindent 0em%
    \itemindent    \listparindent
    \leftmargin    0pt
    \rightmargin   \leftmargin
    \parsep        0pt}%
  \item\relax\myenvstyle}
  {\endlist}

\begin{document}
  \lipsum[11]
  \begin{myenv}
    \lipsum[7]
  \end{myenv}  
  \lipsum[11]
  \begin{mylistenv}
    \lipsum[7]
  \end{mylistenv}
  \lipsum[11]
\end{document}
1

A solution with XeTeX or LuaLaTeX:

\documentclass{article}

\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{xcolor}
\usepackage{lipsum}

%text fonts
\setmainfont{STIX Two Text}
\setsansfont{GFS Neohellenic}
%math fonts
\setmathfont{STIX Two Math}
\setmathfont{GFS Neohellenic Math}[version=sansmath]

\newcounter{myenv}
\newenvironment{myenv}[1][]% environment name
{% begin code
  \par\vspace{\baselineskip}\color{blue}%color of body
  \sffamily\mathversion{sansmath}\noindent
  \refstepcounter{myenv}%
  \textcolor{red}{\textbf{My Enviroment \themyenv\ #1}}%color of title
  \par\noindent\ignorespaces% if you want to indent jus delete \noindent
}%
{% end code
  \par\vspace{\baselineskip}%
  \noindent\ignorespacesafterend%
}

\begin{document}
\lipsum[1]
\[
\sum_{i\in I} A_{i}=\prod_{j\in J}B_j
\]
\begin{myenv}
\label{foo}
\lipsum[2]
\[
\sum_{i\in I} A_{i}=\prod_{j\in J}B_j
\]
\end{myenv}
A reference~\ref{foo}
\end{document}

The output: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.