Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In memoir instead of \raggedright, you can use \raggedyright[<space>] which is much more beautiful in narrow columns. However what I also need is \raggedyleft which does not exist and I don't know why. This is the implementation of \raggedyright in memoir:

\newdimen\ragrparindent
  \setlength{\ragrparindent}{\parindent}
\newcommand{\raggedyright}[1][2em]{%
  \let\\\@centercr\@rightskip \z@ \@plus #1\relax
%%%  \rightskip\@rightskip
  \memRTLrightskip\@rightskip
%%%  \leftskip\z@skip
  \memRTLleftskip\z@skip
  \parindent\ragrparindent}

As someone who does not know TeX macro very much, I could not figure out how it works exactly. I appreciate if someone here can explain me this code and tell me what to do to have \raggedyleft.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

lockstep gave the definition; let's look at what the original code means.

\newdimen\ragrparindent

This sets up a new parameter.

  \setlength{\ragrparindent}{\parindent}

This sets the value; I'd prefer

\AtBeginDocument{
  \setlength{\ragrparindent}{\parindent}
}

but the developers of memoir chose differently.

\newcommand{\raggedyright}[1][2em]{%

This starts the definition of \raggedyright which has an optional argument, with default value 2em

  \let\\\@centercr

This says that \\ must be \@centercr, a wrapper command of the LaTeX kernel used in \raggedright, \centering and \raggedleft for properly ending lines.

  \@rightskip \z@ \@plus #1\relax
%%%  \rightskip\@rightskip
  \memRTLrightskip\@rightskip

The internal parameter \@rightskip is set to a flexible glue with natural width 0pt and stretchability equal to the value given as optional argument. The commented line would set \rightskip to equal \@rightskip; \rightskip is an amount of glue that TeX always adds to the right of the lines (and is usually zero. For compatibility with the bidi package the parameter \memRTLrightskip is set instead (so it will be \rightskip in LTR writing, \leftskip in RTL writing).

%%%  \leftskip\z@skip
  \memRTLleftskip\z@skip

The leftskip is set to zero.

  \parindent\ragrparindent}

This ends the definition, by setting the \parindent (we might be in a situation where the parindent has been changed or cleared).

So, changing what's needed, the definition of \raggedyleft would be

\newcommand{\raggedyleft}[1][2em]{%
  \let\\\@centercr
  \leftskip \z@ \@plus #1\relax
  \memRTLleftskip\leftskip
  \memRTLrightskip\z@skip
  \parindent=\z@
  \parfillskip=\z@skip}

While in a ragged right setting a non zero parindent can be desirable (indeed, it is), in a ragged left setting it's definitely not needed. And there's another adjustment to do: also the final line should be flush right, so also \parfillskip must be set to zero.

There is an asymmetry between left and right, because \rightskip is used also in the trivlist environment and so the kernel needs a "duplicate register" for maintaining a stable value. This isn't the case for \leftskip, so there is no \@leftskip.


A word about \z@, \z@skip and \@plus. The first is a dimension register that's permanently set to 0pt and, by the rules of TeX, can be used in all situations TeX needs a dimension or an integer number (to denote 0pt or 0 respectively). Next, \z@skip is a skip register permanently set to 0pt plus 0pt minus 0pt and can be used to set to zero any skip parameter (rubber length in LaTeXspeak).

\@plus stands for the keyword plus, like \@minus for minus, so

\z@skip\z@ \@plus \z@ \@minus \z@

is the same as saying

\z@skip=0pt plus 0pt minus 0pt

but in a definition the former uses six tokens, while the latter uses 24. When the LaTeX kernel was written, token memory considerations were very important, so it has become customary to still use the "abbreviated forms" that, however, have another advantage: they lift some burden in taking into account spaces that should be present but are eventually ignored.

share|improve this answer
    
+1 for the detailed explanation. You might want to add a bit about \parfillskip\z@skip (which I copied from \raggedleft). –  lockstep Nov 27 '12 at 11:55
    
Also note that the LaTeX2e core doesn't feature \@leftskip, so I think it's correct to simply say \memRTLleftskip\z@ \@plus #1\relax. –  lockstep Nov 27 '12 at 11:57
2  
Thank you very much for the detailed explanation. I should say I am familiar with the syntax and can follow it quite well. I have problems with things like \z@, \@plus. How should one know to use these instead of their equivalent non @ version? Where can I find these kinds of information? In LaTeX2e source? –  rowman Nov 27 '12 at 14:37
1  
@rowman Indeed. Use texdoc source2e to access the source documentation. –  lockstep Nov 27 '12 at 16:29

One basically needs to look up the differences between \raggedright (LaTeX2e core) and \raggedyright (memoir) and then tinker with the definition of \raggedleft {LaTeX2e core) to create \raggedyleft. Note that I didn't implement a \raglparindent length.

\documentclass[11pt]{memoir}

\makeatletter
\newcommand{\raggedyleft}[1][2em]{%
  \let\\\@centercr
  \memRTLleftskip\z@ \@plus #1\relax
  \memRTLrightskip\z@skip
  \parindent\z@
  \parfillskip\z@skip
}
\makeatother

\usepackage{lipsum}

\begin{document}

\raggedleft

\lipsum[1]

\bigskip

\raggedyleft

\lipsum[1]

\bigskip

\raggedyleft[1fil]% same effect as \raggedleft

\lipsum[1]

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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