GeneralHow to remove a leading space when using custom environment?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

How to remove a leading space when using custom environment?

Post by yoyoimut »

Code: Select all

\documentclass[10pt]{article}
\usepackage{xcolor}

%to create framed box that can be splitted across page.
\usepackage{framed}
\renewenvironment{leftbar}[1][\hsize]{%
\def\FrameCommand{{\color{red}\vrule width 5pt}\hspace{0pt}\fboxsep=\FrameSep\colorbox{cyan!50}}%
\MakeFramed {\hsize#1\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}



\newenvironment{Highlight}%
{%
\begin{leftbar}[\linewidth]%
\noindent\small%
}%
{%
\end{leftbar}%
}


\begin{document}
\begin{verbatim}
\begin{Highlight}
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
\end{Highlight}
\end{verbatim}

\begin{Highlight}
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
\end{Highlight}

\noindent\textcolor{red}{ Note: The first row starts with a space.}

\vspace{1cm}

\begin{verbatim}
\begin{Highlight}%
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
\end{Highlight}
\end{verbatim}

\begin{Highlight}%
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
The lazy brown fox jumps quickly over the hotdog.
\end{Highlight}

\noindent\textcolor{red}{ Note: To remove the unnecessary space, I must put \% at the end of opening tag.}

\vspace{1cm}

\noindent\textcolor{blue}{\bf\large Question: Is there a better way to remove the unnecessary space without having to put \% every time I use the environment?}
\end{document}

OUTPUT and explanation :

Image

Thank you in advance.

yoyoimut

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

How to remove a leading space when using custom environment?

Post by phi »

Hello,

use the following pattern with \ignorespaces and \ignorespacesafterend:

Code: Select all

\newenvironment{Highlight}{%
  \leftbar[\linewidth]%
  \noindent\small
  \ignorespaces
}{%
  \endleftbar
  \ignorespacesafterend
}
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

Re: How to remove a leading space when using custom environment?

Post by yoyoimut »

@PHI,



Your code solved my problem.


Thank you.
Post Reply