# “Q” and “A” in margin - out of alignment

I have defined a command for a letter in the margin as follows (using marginnote because marginpar did not work at all):

\newcommand{\sideletter}[1]{\marginnote{\Huge\bf\sffamily\color{orange} #1}}


I appreciate that there are likely going to be issues with vertical alignment, due to the different fonts/sizes and when issuing the commands I have done the following to address the issue:

\sideletter{Q}[-1mm]


and

\sideletter{A}[1mm]


This seems to just about fix the issue vertically. However I cannot seem to align horizontally - the A is just slightly further to the right.

Example image:

Does anyone have any idea about how to approach this? I have tried using hfill and centering but nothing seems to work.

Below is a minimum working example:

\documentclass{article}
\usepackage{lipsum}
\usepackage{marginnote}
\newcommand{\sideletter}[1]{\marginnote{\Huge\bf\sffamily #1}}
\setlength\parindent{0pt}
\begin{document}
\reversemarginpar
\sideletter{Q}
\lipsum[6]

\sideletter{A}
\lipsum[6]
\end{document}


I think in this MWE the misalignment is possibly less obvious, but it is still there. Perhaps the font choice does have some bearing on it, but not everything.

-
Welcome to TeX.sx! I added the image for you. Also, you don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Torbjørn T. Jun 2 '12 at 22:55
Thanks so much! I would suggest that anyone looking at the problem has a look at the full size image if possible - the misalignment is small, but it is real and really bugging me right now. Spent a good hour or so looking for solutions. –  Adam James Hawkins Jun 2 '12 at 22:59
Can you please add to your question a minimal, yet complete document allowing us to reproduce the problem? –  Gonzalo Medina Jun 2 '12 at 23:16

I would go about this a little differently. It seems to me that your 'Q' and 'A' could be wrapped up in a list environment, which makes me immediately think of the enumitem package.

Instead of using a command I have used an environment. Personally I find this preferable, as it may ease global changes later on- for example, you may wish to add numbers to the environment, or perhaps other decorations. See what you think- if it's not appropriate, perhaps someone else will have an alternative.

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{xcolor}     % for colour
\usepackage{enumitem}   % for customizing lists
\usepackage{lipsum}     % for sample text

\newenvironment{question}{\begin{itemize}[leftmargin=0mm]\item[\vbox to -5pt{\vss\hbox{\Huge\bf\sffamily\color{orange} Q}\vss}]}
{\end{itemize}}
{\end{itemize}}

\begin{document}

\begin{question}
\lipsum[1]
\end{question}
\lipsum[1]
\end{document}


If you plan to use \Huge for the question environment, you could use the following for the definition:

\newenvironment{question}{\begin{itemize}[leftmargin=0mm]\item[\Huge\bf\sffamily\color{orange} Q]\Huge}
{\end{itemize}}

-
That looks a very good idea. I will report back shortly. –  Adam James Hawkins Jun 2 '12 at 23:41
Copied your exact MWE & I think it's certainly a more elegant idea than mine, though does not work well when I use \Huge for the question - Q drops down relative to the actual question. Unfortunately the problem remains (and looks similar to my MWE when zoomed in). I believe that my choice of typeface has exaggerated what is already there, but the Q is definitely further to the left than the A (both in XeLaTeX & PDFLaTeX). I expect many would say I'm making a big deal out of nothing, but I'm using LaTeX because I want to get these tiny details right! Thanks - I will experiment with it. –  Adam James Hawkins Jun 3 '12 at 0:01
@AdamJamesHawkins glad it has progressed the problem a bit :) I've updated my answer for the case when you want to use \Huge in the question. With this approach the 'Q' and the 'A' will always be right-aligned. Having them 'left-aligned' or 'centered' might be possible, but it may look a little strange –  cmhughes Jun 3 '12 at 0:13
I think it certainly looks better balanced with your method. The misalignment is still there (as far as my eyes and a zoomed-in PDF can tell) but it is a better solution I feel. Strangely it seems best to put the \Huge inside the environment itself rather than the environment declaration as you suggest. Not sure why there is a difference, but as you suggest increases the line spacing. –  Adam James Hawkins Jun 3 '12 at 0:22
@AdamJamesHawkins: To increase the distance between Q and the question you can use [leftmargin=0mm,labelsep=5mm] in option of \begin{itemize}. Adjust labelsep=5mm accordingly for your taste. –  Harish Kumar Jun 3 '12 at 0:59

Regardless of the method of alignment (e.g., \marginpar, list, etc.) by the end of the day alignment happens by pushing some material towards the edge of some box. This will do proper alignment as long as the material pushed doesn't have any white space included.

With individual characters one may not expect this, but effectively there is and this is simply a feature of the font used. Individual characters are sitting in a bounding box and as far as TeX is concerned such a character is nothing but a rectangle. Whether or not the inkmaking up this character is within the bounds of this box, protruding out of it or even leaving some white space is not know to TeX. So the only remedy here is to visually look at what the font does and correct that.

\Huge\bf\sffamily    \vrule A\vrule Q\vrule


will give you

and as you can see there is different amount of white space around the letters. You can measure that and then manually correct for it using

Q\kern-1pt


or whatever is necessary. (Or on the other side if you try to left-align).

-
That's really interesting. I'm using Helvetica for my sans-serif, but I expect the same is true. I will look into the \kern option. –  Adam James Hawkins Jun 3 '12 at 11:41
Yes the same is true for other fonts (with different amounts), which is why you can't do that automatically from within TeX other than storing a sidebearing table that you have first worked out and then automatically use with some macro that takes your letter as input. And of course, you don't need to use \kern, that is just the low-level TeX command. You could use \hspace{value} if that is more to your taste, but you have to get rid of this white space if you want proper optical alignment. –  Frank Mittelbach Jun 3 '12 at 17:58
 \newcommand{\sideletter}[1]{\marginnote{\Huge\bf\sffamily\color{orange} #1}}


Here the letter will be the start of a paragraph in a fixed width parbox, so it will be flush left in that box and the distance between the letter and the main text will depend on the width and sidebearings of the letter. Also \bf is a deprecated LaTeX2.09 command, better to use bfseries and finally using \color at the start of a parbox can (usually will) cause mis-alignment vertically as it adds a whatsit node to the start of the vertical list which affects the vertical alignment calculations. Better to use \textcolor{orange}{#1} or\noindent\color{orange}#1 so that the color change whatsit gores into the first line of the paragraph rather than before the first line.

As tou want the letter to be strongly bound to the start of the paragraph I wouldn't use a marginpar/parginnote here. Either a list label as suggetsed in the first answer, or simply a box, as below.

\documentclass{article}
\usepackage{lipsum}
\usepackage{color}
\newcommand{\sideletter}[1]{%
\par\makebox(0,0)[r]{\Huge\bfseries\sffamily\textcolor{red}{#1}%
\hspace{4pt}%
}\ignorespaces}
\setlength\parindent{0pt}
\begin{document}

\sideletter{Q}
\lipsum[6]

\sideletter{A}
\lipsum[6]
\end{document}

-
Thanks for the suggestions. I did not know \bf was deprecated - I will avoid it from now on! The alignment is still not perfect, and the more I look at this problem, the more I think it might actually be and issue with the font/letters themselves. Certainly the other answer with the manual adjustments I have made is closest to what I want - I seem to have full x-y control with it. With your solution I cannot work out how to move the letters up and down, although of course the \hspace alters left and right. Thanks! –  Adam James Hawkins Jun 3 '12 at 11:34
as you say the hspace moves left/right to move up and down you coud use \raisebox{2pt}{#1} instead of # or in fact wrap the \makebox call in \begin{picture}(0,0)\put(10,10){\makebox....}\end{picture} then by altering the put coordinates you could put the letter anywhere on the page. Especially with A you are likely to need per-letter tweaking. In a normal text contents the font will supply inter-letter kerns to adjust for the visual gaps caused by letter shapes, but when changing fonts and positioning one big letter you have to do it by hand. –  David Carlisle Jun 3 '12 at 11:42
Noted. I will be sticking with the Environment/Itemize solution as I have modified it to align correctly, but I think with that tweaking this one would be just as good. I suppose the only other way would be to have centering, but that hasn't worked so far! Thanks. –  Adam James Hawkins Jun 3 '12 at 11:57
Can you please explain what you mean by "Here the letter will be the start of a paragraph in a fixed width parbox"? Do you mean the Q resp. A? Then I don't see in which sense it's a fixed width. –  Hendrik Vogt Jun 3 '12 at 14:54
\marginnote is (I think) like \marginpar in that is argument is set as a parbox of fixed width isn't it? –  David Carlisle Jun 3 '12 at 16:08

As far as I see, noone has explained the reason for your alignment problem yet. The bounding boxes mentioned by Frank Mittelbach do play a role (see below), but the more important point is that \marginnotes in the left margin are typeset \raggedleft (so that you have right alignment). So it's very much by design that the A is slightly further to the right: the Q is a bit wider and thus protrudes more to the left.

As explained in the marginnote documentation, you could switch to \raggedright:

\renewcommand*{\raggedleftmarginnote}{\raggedright}


But this is not a good idea since you'll get too much space between the \marginnote and the main text. A better solution would be to use

\newcommand{\sideletter}[1]{\marginnote{\Huge\bf\sffamily\rlap{#1}\phantom{Q}}}


(where I neglected that \bf is deprecated; I just wanted to stay close to the original code). This produces a \marginnote that has the width of the letter Q, and the letter you want is put flush left. If you look closely, the A will then be slightly further to the left:

This is explained in Frank's answer: The Q just has a bit more white space to the left in its bounding box. And again, this is by design! There's only a tiny bit of the A that protrudes to the left, whereas the Q has really a lot of black in the left, so visually you have appropriate left alignment.

-
Thanks - that's really enlightening. –  Adam James Hawkins Jun 3 '12 at 15:52

This one seems to work with marginpar:

\documentclass[11pt]{article}
\usepackage{xcolor,blindtext}
\newcommand*{\noPlace}{\hrule height0pt width 0pt}
\reversemarginpar
\newcommand{\QA}[2]{%
\mbox{}\leavevmode\marginpar{\textcolor{orange}{%
\noPlace\sffamily\Huge#1}}%
{\noPlace#2}\par}
\setlength{\marginparwidth}{0.2in}%
\setlength{\parindent}{0pt}

\begin{document}
\QA{Q}{\blindtext}

\QA{A}{\blindtext}

\end{document}


-
A neat solution - I like how it's all rolled into one. I'm afraid that I still have the minimal issue of the alignment, which as @FrankMittelbach has suggested is due to the font itself. I think I will be sticking with the "environment" solution as I have managed to tweak it for spacing, but I think this would do equally well. Thanks! –  Adam James Hawkins Jun 3 '12 at 11:55