Graphics, Figures & TablesGraphical size (drawing long line at the baseline of a word)

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Graphical size (drawing long line at the baseline of a word)

Post by karlisrepsons »

All,
perhaps someone of you might suggest how to get a long line and a word on it so that chars like 'a' are above that line, but 'p' or 'ņ' are split? I got the following code to do almost what was intended, but it has a problem with incorrect spacing and if similar construct is used within a tabu environment (possibly elsewhere too), strange problems appear:

Code: Select all

\documentclass{article}

\usepackage{geometry}
\geometry{papersize={16cm, 4cm}, inner=0cm,outer=0cm,top=0cm,bottom=0cm}

\usepackage{xcolor}
\usepackage{tabu}
\usepackage{fontspec}

\begin{document}

\begin{center}
   \color{blue}
   \fontsize{90}{0}%
   \fontspec{Times New Roman}%
%    \vspace*{1em}%
   \raisebox{0pt}[\height][0pt]{\makebox[14cm][c]{Kociņš}}%
   \par\nointerlineskip%
   \color{red}%
   \makebox[14cm]{\hrulefill}%
\end{center}

\end{document}
I was thinking to me: "How good it would be, had LaTeX a way to measure the graphical dimensions of some contents not counting any surrounding whitespaces..." Are things bad enough for LaTeX users not to have an exact width, height and depth of, say, a character like 'ļ'?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10322
Joined: Mon Mar 10, 2008 9:44 pm

Graphical size (drawing long line at the baseline of a word)

Post by Stefan Kottwitz »

Hi,

you could use \settowidth.

Stefan
LaTeX.org admin
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Graphical size (drawing long line at the baseline of a word)

Post by karlisrepsons »

I could? Well yes, take a look how mightily bad it all works:

Code: Select all

\documentclass{article}

\usepackage{geometry} % just to set up the page margin distances
\usepackage{array} % already a reworked macros base for tabu
\usepackage{xcolor} % to have colors, also required by tabu
\usepackage{tabu} % the best tables-generator macros I know for LaTeX
\usepackage{fontspec}   % provides font selecting commands
\usepackage{calc}

\geometry{papersize={5cm, 5cm}, inner=4mm,outer=4mm,top=4mm,bottom=4mm}

\definecolor{myblue}{rgb}{0, 0.4, 0.7}
\definecolor{mygreen}{rgb}{0, 0.7, 0.4}
\definecolor{myred}{rgb}{0.7, 0.4, 0}

\defaultfontfeatures{Mapping=tex-text}
\setmainfont{Times New Roman}
\newcommand{\textmode} {%
   \color{myblue}%
   \fontsize{40}{0}%
   \bfseries \itshape%
}

\newcommand{\quickAnalyzeSavebox}[1]{%
   \newlength{\widthB}%
   \newlength{\heightB}%
   \newlength{\depthB}%
   \newlength{\HeightB}%
   \settowidth{\widthB}{\usebox{#1}}%
   \settoheight{\heightB}{\usebox{#1}}%
   \settoheight{\depthB}{\usebox{#1}}%
   \setlength{\HeightB}{\heightB + \depthB}%
   \tabulinesep=0pt
   \tabcolsep=0pt
   \begin{tabu} spread 0pt {cc}
      \color{myred} \vrule width 1pt height 0pt depth \depthB \relax%
      { }%
      \color{mygreen} \vrule width 1pt height \heightB depth 0pt \relax%
      { }%
      \color{myblue} \vrule width 1pt height \heightB depth \depthB \relax%
      { }%
      \color{pink} \vrule width 1pt height \HeightB depth 0pt \relax%
      \hspace*{1ex}%
      &
      \color{black}%
      \parbox{\widthB}{%
         \makebox[\widthB]{\hrulefill} \\%
         \usebox{#1} \\%
         \makebox[\widthB]{\hrulefill}%
      }
   \end{tabu}%
}

\begin{document}
\thispagestyle{plain}
\newsavebox{\analyzeBox}
\savebox{\analyzeBox}{\textmode Ā}
\quickAnalyzeSavebox{\analyzeBox}
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10322
Joined: Mon Mar 10, 2008 9:44 pm

Graphical size (drawing long line at the baseline of a word)

Post by Stefan Kottwitz »

I had a look at the output. Whatever you are doing...

If I just write, let's say

Code: Select all

\color{myred}\rule{1pt}{\heightB}\rlap{\rule{\widthB}{1pt}}\usebox{#1}
I get the attached output, which looks meaningful.
example
example
A.png (3.32 KiB) Viewed 3854 times
Stefan
LaTeX.org admin
karlisrepsons
Posts: 50
Joined: Sat May 23, 2009 10:13 pm

Graphical size (drawing long line at the baseline of a word)

Post by karlisrepsons »

Hmm, looks better now: with your idea I made another test, which can produce the following picture...
this look fine...
this look fine...
ok.jpeg (11.64 KiB) Viewed 3854 times
not precisely for width, but close...
not precisely for width, but close...
Ā reg.jpeg (7.32 KiB) Viewed 3854 times
If italic is used, the situation gets worse however (!zoom problem in effect!):
not so good at all...
not so good at all...
Ā italic.jpeg (5.15 KiB) Viewed 3854 times
Also I didn't realize that my PDF viewer doesn't show correctly while not zoomed close enough (Adobe Acrobat 9, well)...

This could be produced with the following code:

Code: Select all

\documentclass{article}

\usepackage{geometry} % just to set up the page margin distances
\usepackage{xcolor} % to have colors, also required by tabu
\usepackage{fontspec}   % provides font selecting commands
\usepackage{calc}

\geometry{papersize={11cm, 7cm}, inner=4mm,outer=4mm,top=4mm,bottom=4mm}

\definecolor{myblue}{rgb}{0, 0.4, 0.7}
\definecolor{mygreen}{rgb}{0, 1, 0}
\definecolor{myred}{rgb}{1, 0, 0}
\definecolor{myorange}{rgb}{1, 0.7, 0}

\defaultfontfeatures{Mapping=tex-text}
\setmainfont{Times New Roman}
\newcommand{\textmode} {%
   \color{myblue}%
   \fontsize{160}{0}%
   \bfseries%
   %\itshape
}

\newlength{\widthB}%
\newlength{\heightB}%
\newlength{\depthB}%

\newlength{\thicknessLine}
\setlength{\thicknessLine}{2pt}

\newcommand{\bAnalyze}[1]{%
   \settowidth{\widthB}{\usebox{#1}}%
   \settoheight{\heightB}{\usebox{#1}}%
   \settodepth{\depthB}{\usebox{#1}}%
   \color{myorange}%
   \usebox{#1}%
   \llap{%
      \rule[-\thicknessLine]{\widthB}{\thicknessLine}%
   }%
   \color{mygreen}%
   \vrule width \thicknessLine height \heightB depth 0pt \relax%
   \color{myred}%
   \llap{%
      \vrule width \thicknessLine height 0pt depth \depthB \relax%
   }%
}

\begin{document}
\thispagestyle{plain}
\newsavebox{\analyzeBox}
\savebox{\analyzeBox}{\textmode{}Ā}
\bAnalyze{\analyzeBox}
\end{document}
So the question is in part still valid: can I get exact dimensions of some (whatever) element (glyph, table, picture etc), meaning the visible width, height and depth?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10322
Joined: Mon Mar 10, 2008 9:44 pm

Graphical size (drawing long line at the baseline of a word)

Post by Stefan Kottwitz »

With the mentioned commands you get the size of the box, not the size of the area which might be touched by the box contents. Commands such as \rlap, \llap and \smash change the box but not the contents. Hard to measure a box later if anything inside could have happened.

For typesetting usually the box matters - not if there's something overlapping because of italic or slanted font or because of kerning. The next letter follows the box. Here's the italic example showing what I mean.
box example
box example
box.png (4.66 KiB) Viewed 3842 times
Just to explain what \settowidth and \settoheight are expected to do. That's not a solution for measuring the area where ink gets on the paper.

Stefan
LaTeX.org admin
Post Reply