Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculate the width of a LaTeX string in C code

8 views
Skip to first unread message

Cameron Bracken

unread,
Jul 9, 2009, 6:22:56 PM7/9/09
to
In a project I am working on I need to calculate the width of an
arbitrary LaTeX string from C. For the sake of argument say that we
are using the default LaTeX fonts and point sizes. I would like it to
be as easy as:

width = getWidthOfLatexString("{\\tiny Hello} \\LaTeX{}!");

The output dimensions would be best in inches, centimeters or a
fraction of the page width. Ideally I would also like to be able to
handle UTF8 strings as well. Does anyone know how I can accomplish
this?

Thanks,

Cameron

Heiko Oberdiek

unread,
Jul 9, 2009, 9:29:15 PM7/9/09
to
Cameron Bracken <cameron...@gmail.com> wrote:

Write a small LaTeX file, put your string into a box:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
... other font setup stuff
\sbox0{{\tiny Hello} \LaTeX!}
\typeout{width=\the\wd0}
% end job, no need for output file
\makeatletter
\@@end

run LaTeX and analyze the .log file or screen output for the wanted
width data.

Yours sincerely
Heiko <ober...@uni-freiburg.de>

Marc van Dongen

unread,
Jul 10, 2009, 9:32:39 AM7/10/09
to
On Jul 10, 2:29 am, Heiko Oberdiek <oberd...@uni-freiburg.de> wrote:


> Write a small LaTeX file, put your string into a box:
>
> \documentclass{article}
> \usepackage[utf8]{inputenc}
> \usepackage[T1]{fontenc}
> ... other font setup stuff
> \sbox0{{\tiny Hello} \LaTeX!}
> \typeout{width=\the\wd0}
> % end job, no need for output file
> \makeatletter
> \@@end
>
> run LaTeX and analyze the .log file or screen output for the wanted
> width data.

That may not always work as it may depend on the context/environment
(I've been struggling with this for the past two days:-).

For example, in the following, the width that is output is 0.0pt
inside the tikzpicture and 47.16391pt outisde the tikzpicture. It
seems difficult to make the let-LaTeX-do-the-job more robust as
Cameron's program would have to know exactly what is going on. The
fact that LaTeX/TeX can construct command sequences on the fly (using
\csname), branching, and loops, seems to make the job only more
difficult.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}


\sbox0{{\tiny Hello} \LaTeX!}
\typeout{width=\the\wd0}

\end{tikzpicture}


\sbox0{{\tiny Hello} \LaTeX!}
\typeout{width=\the\wd0}

\end{document}

Regards,


Marc van Dongen

Cameron Bracken

unread,
Jul 10, 2009, 10:19:24 AM7/10/09
to
On Jul 10, 6:32 am, Marc van Dongen <don...@cs.ucc.ie> wrote:

> For example, in the following, the width that is output is 0.0pt
> inside the tikzpicture and 47.16391pt outisde the tikzpicture. It

Fortunately Heiko's solution will work for me. I will be constructing
a new file for every string so I shouldn't run onto the problem that
you describe.

This is a great solution, much easier than I thought it would be.

Oliver Corff

unread,
Jul 11, 2009, 6:58:24 AM7/11/09
to
Cameron Bracken <cameron...@gmail.com> wrote:
: In a project I am working on I need to calculate the width of an

It was pointed out already to have LaTeX acquire the necessary
information. If you deal with pure textual strings (your example
however implies things like \LaTeX{} as well) AND want to stay
within a pure C programming environment you can choose the font
in which your material is expressed, convert the metrics to something
accessible in text/list form (use tftopl) and read the width data
of your property list into an array from where to calculate the length
of your string. Two caveats: You have to take into account the ligature
data and you have to calculate scaling factors for things like \tiny.

Oliver.

--
Dr. Oliver Corff e-mail: co...@zedat.fu-berlin.de

Peter Flynn

unread,
Jul 15, 2009, 6:36:32 PM7/15/09
to
Cameron Bracken wrote:
> In a project I am working on I need to calculate the width of an
> arbitrary LaTeX string from C. For the sake of argument say that we
> are using the default LaTeX fonts and point sizes. I would like it to
> be as easy as:
>
> width = getWidthOfLatexString("{\\tiny Hello} \\LaTeX{}!");

\newdimen{\fragwidth}
\settowidth{\fragwidth}{{\\tiny Hello} \\LaTeX{}!}

///Peter

0 new messages