25

I am trying to align a series of pictures made up of letters, using TikZ. Having tried almost all combinations of the baseline key it is obvious I am missing something. Here is a MWE (currently adjusting with an approximate manual baseline=-4pt). It is obvious from the image that the adjustment only applies to the first letter. How can I fix this?

enter image description here

\documentclass{article}
\usepackage{pgf,tikz}
\makeatletter
\begin{document}
\gdef\drawfontframe#1{%
  \tikz[baseline=-4pt]{%
    \node[rectangle,draw,inner sep=0pt,outer sep=0pt] (X){#1};
    \draw[red, line width=0.4pt] (X.text)  circle(0.4pt)[fill=red] -- (X.base east);}%
}
\Huge A test %
\@tfor\next:=qwerty\do{%
\def\Z#1{\drawfontframe{#1}}%
\expandafter\Z\expandafter{\next}.%
}
\end{document}
6
  • 3
    You can use: \tikz[baseline=(X.base)]{ Jun 2, 2012 at 15:15
  • I tried (all permutations) it gives an error:) Jun 2, 2012 at 15:24
  • I am using the cvs version of TikZ and it works well. Jun 2, 2012 at 15:32
  • 1
    @MarcoDaniel I kept on typing {X.base} rather than (X.base). You right it does work. Jun 2, 2012 at 15:35
  • 1
    Also see tex.stackexchange.com/q/58419/86 for another solution which avoids the necessity of naming the nodes. Jun 3, 2012 at 20:10

2 Answers 2

24

As mentioned in the comments,

  \tikz[baseline=(X.base)]{%

works for this

enter image description here

\documentclass{article}
\usepackage{pgf,tikz}
\makeatletter
\begin{document}
\gdef\drawfontframe#1{%
  \tikz[baseline=(X.base)]{%
    \node[rectangle,draw,inner sep=0pt,outer sep=0pt] (X){#1};
    \draw[red, line width=0.4pt] (X.text)  circle(0.4pt)[fill=red] -- (X.base east);}%
}
\Huge A test %
\@tfor\next:=qwerty\do{%
\def\Z#1{\drawfontframe{#1}}%
\expandafter\Z\expandafter{\next}.%
}
\end{document}

Here's the output from \listfiles for reference (I'm working on an up to date TeXLive 2011 on Ubuntu)

 *File List*
 article.cls    2007/10/19 v1.4h Standard LaTeX document class
  size10.clo    2007/10/19 v1.4h Standard LaTeX file (size option)
     pgf.sty    2008/01/15 v2.10 (rcs-revision 1.12)
  pgfrcs.sty    2010/10/25 v2.10 (rcs-revision 1.24)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v2.10 (rcs-revision 1.7)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2010/04/23 v1.9 graphics configuration of TeX Live
  pdftex.def    2011/05/27 v0.06d Graphics/color for pdfTeX
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
 ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)
  pgfsys.sty    2010/06/30 v2.10 (rcs-revision 1.37)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2008/07/18  (rcs-revision 1.7)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2007/01/21 v2.11 LaTeX color extensions (UK)
   color.cfg    2007/01/18 v1.5 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v2.10 (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v2.10 (rcs-revision 1.1)
    tikz.sty    2010/10/13 v2.10 (rcs-revision 1.76)
  pgffor.sty    2010/03/23 v2.10 (rcs-revision 1.18)
 pgfkeys.sty    
 pgfkeys.code.tex
  pgffor.code.tex
    tikz.code.tex
supp-pdf.mkii
pdftexcmds.sty    2011/11/29 v0.20 Utility functions of pdfTeX for LuaTeX (HO)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
   ifpdf.sty    2011/01/30 v2.3 Provides the ifpdf switch (HO)
epstopdf-base.sty    2010/02/09 v2.5 Base part for package epstopdf
  grfext.sty    2010/08/19 v1.1 Manage graphics extensions (HO)
kvdefinekeys.sty    2011/04/07 v1.3 Define keys (HO)
kvoptions.sty    2011/06/30 v3.11 Key value format for package options (HO)
kvsetkeys.sty    2012/04/25 v1.16 Key value parser (HO)
etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
 ***********
4
  • Strange I kept on getting ! Package PGF Math Error: Unknown function X' (in 'X.base'), but just realized I was typing {X.base}`, i.e., kept on using curly brackets. Thanks. Jun 2, 2012 at 15:32
  • @YiannisLazarides I added the output from \listfiles for reference
    – cmhughes
    Jun 2, 2012 at 15:35
  • 4
    A better way to do this (in my opinion) is to add anchor=base to the nodes you want to align, and baseline to the tikzpicture. That saves you the trouble of having to name the nodes just to align them.
    – Jake
    Jun 3, 2012 at 19:54
  • @Jake thanks for the feedback- why not post an answer, I'd vote for it
    – cmhughes
    Jun 3, 2012 at 21:50
8

Here a solution based on Jake's comment.

Remarks : It's not necessary to load pgf, then without the circle and the line inside the node, the solution is easy and it's possible to use the node without a name.

Without a name it's difficult to draw the line, we need to use current bounding box but it's necessary to remove .5\pgflinewidth at each side.

\documentclass{article}
\usepackage{tikz}
\makeatletter
\begin{document}

\gdef\drawfontframe#1{%
  \tikz[baseline]{%
  \node[anchor=base,draw,inner sep=0pt,outer sep=0pt] {#1};
  \filldraw[red, line width=0.4pt] ([xshift=.5\pgflinewidth]current bounding box.base west) circle(1pt)
    -- ([xshift=-.5\pgflinewidth]current bounding box.base east);
    }%
}%

\Huge A test %
\@tfor\next:=qwerty\do{%
\def\Z#1{\drawfontframe{#1}}%
\expandafter\Z\expandafter{\next}.%
}
\end{document}

enter image description here

You must log in to answer this question.

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