GeneralHow to print out the value of \textwidth?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How to print out the value of \textwidth?

Post by fractal.generator »

Dear all,

I want to print out the value of \textwidth in the =? placeholder as follows

Code: Select all

\newcommand*{\MARKER}%
  {\noindent\strut\vrule
   \hrulefill~half textwidth =? cm~\hrulefill\vrule
   \hrulefill~half textwidth =? cm~\hrulefill\vrule
    \marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}
Please help me.


Thank you in advance.

regards,

Hayashi

Recommended reading 2024:

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

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

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to print out the value of \textwidth?

Post by Juanjo »

Code: Select all

\the\textwidth
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How to print out the value of \textwidth?

Post by fractal.generator »

Juanjo wrote:

Code: Select all

\the\textwidth
Mr. Juanjo,
Thanks for replying.

Is it possible to convert the unit from pt to cm?

Thanks in advance.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10337
Joined: Mon Mar 10, 2008 9:44 pm

How to print out the value of \textwidth?

Post by Stefan Kottwitz »

Hi,

as I don't know a more direct way I've made a calculation and manipulated it's output, here's a demonstration example:

Code: Select all

\documentclass[a4paper,10pt]{article}
\setlength{\textwidth}{14cm}
\newlength\textwidthcm
\textwidthcm=.03514598035146\textwidth
\begin{document}
{\catcode`p=12 \catcode`t=12 \gdef\cm#1pt{#1cm}}
\begin{tabular}{ll}
textwidth in pt: \the\textwidth \\
textwidth in cm: \expandafter\cm\the\textwidthcm
\end{tabular}
\end{document}
Output is 13.998, so there may be a small rounding error, I just used 72.27pt=1in and 1in=2.54cm for the calculation.
But there are packages that are capable of floating point operations.

Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to print out the value of \textwidth?

Post by Juanjo »

With the help of the \catcode trick given by Stephan_K and the fp package, it is possible to define macros to convert from one unit to another:

Code: Select all

\documentclass[a4paper]{article}
\usepackage{fp}

\newlength{\TOarg} \newlength{\TOunit}
{\catcode`p=12 \catcode`t=12 \gdef\TOnum#1pt{#1}}
\newcommand\TOop[2]{\setlength{\TOarg}{#2}%
   \FPdiv\TOres{\expandafter\TOnum\the\TOarg}{\expandafter\TOnum\the\TOunit}%
   \FPround\TOres\TOres{#1}}
\newcommand{\TOspace}{\ } 
\newcommand\TOpt[2][2]{\setlength{\TOunit}{1pt}\TOop{#1}{#2}\TOres\TOspace pt}
\newcommand\TOin[2][2]{\setlength{\TOunit}{1in}\TOop{#1}{#2}\TOres\TOspace in}
\newcommand\TOcm[2][2]{\setlength{\TOunit}{1cm}\TOop{#1}{#2}\TOres\TOspace cm}
\newcommand\TOmm[2][2]{\setlength{\TOunit}{1mm}\TOop{#1}{#2}\TOres\TOspace mm}
\newcommand\TOem[2][2]{\setlength{\TOunit}{1em}\TOop{#1}{#2}\TOres\TOspace em}

\begin{document}
The width of this document is \TOpt[0]{\textwidth}, that is, \TOcm{\textwidth}, whereas the height is \TOcm[3]{\textheight}, i.e. \TOin{\textheight}. Here we have some equivalences between different units:

\begin{center}
   \renewcommand{\arraystretch}{1.5}
   \begin{tabular}{r*{4}{@{\ =\ }r}}
      1pt & \TOpt[0]{1pt} & \TOin[3]{1pt} & \TOcm{1pt} & \TOmm{1pt}  \\ 
      1in & \TOpt{1in} & \TOin[0]{1in} & \TOcm{1in} & \TOmm[1]{1in}  \\
      1cm & \TOpt{1cm} & \TOin[3]{1cm} & \TOcm[0]{1cm} & \TOmm[0]{1cm}  \\
      1mm & \TOpt{1mm} & \TOin[3]{1mm} & \TOcm{1mm} & \TOmm[0]{1mm}  
   \end{tabular}
\end{center}

The em unit depends on which font is active:
\begin{itemize}
   \item 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}
   \item {\bfseries 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
   \item {\bfseries \large 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
   \item {\ttfamily 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
\end{itemize}
\end{document}
The optional argument of TOpt, TOcm and like fixes the number of decimal digits shown (2 by default). These macros first convert their mandatory argument to pt (done automatically by TeX) and then the conversion to the corresponding unit.
From the above code, it is easy to add macros for conversion to pc, ex, bp and so on.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10337
Joined: Mon Mar 10, 2008 9:44 pm

How to print out the value of \textwidth?

Post by Stefan Kottwitz »

Hi Juanjo,

you've given a very good demonstration!
Btw. I've read the catcode trick in the TeXbook Appendix D: Dirty Tricks.

Stefan
LaTeX.org admin
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

Re: How to print out the value of \textwidth?

Post by fractal.generator »

To Stefen and Juanjo,

thanks for replying.


I am trying to diggest the codes :lol:



Hayashi
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to print out the value of \textwidth?

Post by Juanjo »

Stefan_K wrote:Hi Juanjo,

you've given a very good demonstration!
Btw. I've read the catcode trick in the TeXbook Appendix D: Dirty Tricks.

Stefan
Well, not so good, since I've reinvented the wheel! I've found that the layouts package defines the commands \printinunitsof and \prntlen. The latter prints the value of a length in the units specified by the former. So, for example, after loading layouts, the code

Code: Select all

\printinunitsof{cm}\prntlen{\textwidth} \prntlen{\textheight}
prints the value of \textwidth and \textheight expressed in cm. Reading layouts.sty, one sees that conversion is internally done by multiplication by a suitable factor (without the help of fp). The code there uses the internal LaTeX macro \strip@pt, similar to (but much more elaborated than) \TOnum in my code. In fact, with the help of this macro, my code can be simplified: \TOnum is unnecessary and \TOop can be given by

Code: Select all

\makeatletter
\newcommand\TOop[2]{\setlength{\TOarg}{#2}\FPdiv\TOres{\strip@pt\TOarg}{\strip@pt\TOunit}\FPround\TOres\TOres{#1}}
\makeatother
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How to print out the value of \textwidth?

Post by fractal.generator »

Dear all,

By accident, I found a package about this topic:
http://www.ctan.org/tex-archive/help/Ca ... ntlen.html


regards,


Hayashi
Post Reply