1

Here is my simplest file where I see this problem:

\documentclass[article,titlepage,12pt,openany,draft]{memoir}

\usepackage{calc}
\usepackage[dvipsnames]{xcolor}
\definecolor{shadecolor}{gray}{0.85}
\usepackage{lipsum}


\newlength{\shademar}
\setlength{\shademar}{2.5\FrameSep}

\newenvironment*{ShadeAux}[2]%
{\begin{adjustwidth}{\shademar + #1}{\shademar + #2}\begin{qshade}\ignorespaces}%
{\end{qshade}\end{adjustwidth}\ignorespacesafterend}


\begin{document}

\lipsum[3]
\begin{ShadeAux}{\leftmargin}{0pt}
\lipsum[12]
\end{ShadeAux}
\begin{quotation}
\lipsum[12]
\end{quotation}
\begin{ShadeAux}{\leftmargin}{\leftmargin}
\lipsum[12]
\end{ShadeAux}
\lipsum[3]

\end{document}

The first length \shademar is there to remove the protrusion of the qshade environment into the margins. As you can see, the first call to ShadeAux works as expected. But the second call to ShadeAux indents inward the right margin more than it should.

As a response to the comments: the reason I did not initially add \shademar before the àdjustwidth` environment is that I am trying to be clever and not have two define two (or one) extra length: i.e. one for the left margin, one for the right margin.

Why is it going wrong?

7
  • Why not save yourself the trouble and do \setlength{\shademar}{\dimexpr 2.5\FrameSep + \leftmargin\relax} and then simply \begin{adjustwidth}{\shademar}{\shademar}?
    – jon
    Dec 1, 2016 at 4:06
  • @jon In fact \leftmargin is meant to take the place of an adjustable margin (which is to say, a variable length accessed by #1, for instance). In reality ShadeAux is a function with two arguments. But since I am supposed to submit a minimal example, I abstracted away the functional part, and give an example which shows that the error happens even outside the context of a function.
    – ltcomdata
    Dec 1, 2016 at 4:41
  • @jon what is the function of \dimexpr?
    – ltcomdata
    Dec 1, 2016 at 4:44
  • \dimexpr evaluates an expression for dimen (requires e-TeX). But I still don't understand what you're trying to do or what \leftmargin is doing in the calculation anyway: if you remove it from both sides and simply use \begin{adjustwidth}{\shademar}{\shademar}, with or without my \setlength suggestion above, you get the same result.
    – jon
    Dec 1, 2016 at 5:25
  • the two instances of \leftmargin have different values as adjustwidth sets \leftmargin based on the first argument, and then you use it again in the second. You should do the arithmetic before calling the environment as jon said. Dec 1, 2016 at 9:21

1 Answer 1

1

adjustwidth sets \leftmargin so when it comes to look at the second argument the input value has already changed, if you evaluate both lengths first you get a different result (hopefully the one you intended)

enter image description here

\documentclass[article,titlepage,12pt,openany,draft]{memoir}

\usepackage{calc}
\usepackage[dvipsnames]{xcolor}
\definecolor{shadecolor}{gray}{0.85}
\newlength\zzza
\newlength\zzzb
\usepackage{lipsum}


\newlength{\shademar}
\setlength{\shademar}{2.5\FrameSep}

\newenvironment*{ShadeAux}[2]%
{%
\setlength\zzza{\shademar+#1}%
\setlength\zzzb{\shademar+#2}%
\begin{adjustwidth}{\zzza}{\zzzb}\begin{qshade}\ignorespaces}%
{\end{qshade}\end{adjustwidth}\ignorespacesafterend}


\begin{document}

\lipsum[3]
\begin{ShadeAux}{\leftmargin}{0pt}
\lipsum[12]
\end{ShadeAux}
\begin{quotation}
\lipsum[12]
\end{quotation}
\begin{ShadeAux}{\leftmargin}{\leftmargin}
\lipsum[12]
\end{ShadeAux}
\lipsum[3]

\end{document}
1
  • Thank you for your response. I noticed a small error in your code. You should have \setlength\zzzb{\shademar+#2} instead of the line you have with the #1. Otherwise it works. I had not considered that adjustwidth would change the value of leftmargin between the left-margin use and the right-margin use.
    – ltcomdata
    Dec 5, 2016 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .