2

I want to use an environment defined in a 3rd-party package which I cannot modify. Stripped down, the definition looks like this:

\newenvironment{myenv}[1]{
% some stuff here
\textbf{#1}
}{% some more stuff here
}

Now I would like to use this environment without the argument (or rather with an empty argument). However, there is still some space (originating from the line breaks in the definition of the environment) at the beginning of the line. I would like to get rid of this space.

How can I make the content of the environment start at the beginning of the line without modifying the definition of the environment?

Here's a minimal example:

\documentclass{article}
% I cannot make changes from here ...
\newenvironment{myenv}[1]{
\textbf{#1}
}{}
% ... to here.
\begin{document}
\begin{myenv}{Argument}
Content
\end{myenv}

\begin{myenv}{}
Content Only
\end{myenv}
\end{document}

I would like the second line to begin at the same horizontal position as the first line.

I could of course use a negative \hspace{} of some fixed size, but I would like this to work for arbitrary font settings etc.

BTW, I don't care if the solution is ugly, since the LaTeX code will be auto-generated by a script.

  • 1
    \newenvironment{myenv}[1]{%, but you can't change the environment. But you can redefine it afterwards – user31729 Apr 23 '16 at 9:35
  • 1
    try \begin{myenv}{}\unskip Content Only \end{myenv} – touhami Apr 23 '16 at 9:39
  • 1
    @touhami: \textbf issues \leavevmode (inside \hmode@bgroup), and this is irreversible. – GuM Apr 23 '16 at 9:46
  • Thanks @touhami your suggestion with \unskip works perfectly! If you add this as an answer, I'll accept it. – Matthias Apr 23 '16 at 10:17
5

enter image description here

It is possible to change the definition with a patch:

There are two issues here

  1. The missing % after \textbf{#1}
  2. An empty {} causes spacing due to \textbf{}

I've added a \string% and a check whether the argument is empty -- \notblank{#1}{}{} checks if the #1 is empty and if this is not the case does the code in the 2nd argument.


\documentclass{article}
\usepackage{xpatch}%
% I cannot make changes from here ...
% Some statements by me (C.H.)
\newenvironment{myenv}[1]{% is missing
  \textbf{#1}% is missing
}{}
% ... to here.


\xpatchcmd{\myenv}{\textbf{#1}}{\notblank{#1}{\textbf{#1}}{}\string%
}{\typeout{Success}}{\typeout{Failed!}}

\begin{document}

Compare

\textbf{Argument}

Content only

\begin{myenv}{Argument}
  Content
\end{myenv}

\begin{myenv}{}
  Content Only
\end{myenv}
\end{document}
  • Other possibility: Redefine the environment to drop the argument (i.e. use it with empty argument) – user31729 Apr 23 '16 at 9:55
  • Thanks a lot for this solution! However, the much simpler \unskip solution from @touhami also seems to do the trick. Is there any reason not to use it? – Matthias Apr 23 '16 at 10:19
  • @Matthias: It's not guaranteed to be working always, I think – user31729 Apr 23 '16 at 10:20
  • OK, thanks! I'll use \unskip for now but if it turns out to cause problems, I'll switch to your solution. – Matthias Apr 23 '16 at 10:43

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.