Featured Book

LaTeX Beginners Guide

Who is online

In total there are 23 users online :: 2 registered, 0 hidden and 21 guests (based on users active over the past 5 minutes)
Most users ever online was 1327 on Tue Nov 05, 2013 7:11 pm

Users browsing this forum: Majestic-12 [Bot], Yahoo [Bot] and 21 guests

Partner Sites

TeXwelt - Fragen und Antworten goLaTeX
TeXblog
TeXnicCenter
TeXample

How to print out the value of \textwidth?

Add tags LaTeX specific issues not fitting into one of the other forums of this category.

How to print out the value of \textwidth?

Postby fractal.generator on Fri Oct 10th, 2008

Dear all,

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

Code: Select all  •  Open in writeLaTeX
\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
fractal.generator
 
Posts: 66
Joined: Tue Oct 7th, 2008
Location: Japan

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

Postby Juanjo on Fri Oct 10th, 2008

Code: Select all  •  Open in writeLaTeX
\the\textwidth
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Juanjo
 
Posts: 656
Joined: Sat Jan 27th, 2007

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

Postby fractal.generator on Fri Oct 10th, 2008

Juanjo wrote:
Code: Select all  •  Open in writeLaTeX
\the\textwidth


Mr. Juanjo,
Thanks for replying.

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

Thanks in advance.
fractal.generator
 
Posts: 66
Joined: Tue Oct 7th, 2008
Location: Japan

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

Postby Stefan_K on Fri Oct 10th, 2008

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  •  Open in writeLaTeX
\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 Community Administrator
User avatar
Stefan_K
Site Admin
 
Posts: 6413
Joined: Mon Mar 10th, 2008
Location: Hamburg, Germany

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

Postby Juanjo on Sat Oct 11th, 2008

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  •  Open in writeLaTeX
\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
Juanjo
 
Posts: 656
Joined: Sat Jan 27th, 2007

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

Postby Stefan_K on Sat Oct 11th, 2008

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 Community Administrator
User avatar
Stefan_K
Site Admin
 
Posts: 6413
Joined: Mon Mar 10th, 2008
Location: Hamburg, Germany

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

Postby fractal.generator on Sat Oct 11th, 2008

To Stefen and Juanjo,

thanks for replying.


I am trying to diggest the codes :lol:



Hayashi
fractal.generator
 
Posts: 66
Joined: Tue Oct 7th, 2008
Location: Japan

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

Postby Juanjo on Mon Oct 13th, 2008

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  •  Open in writeLaTeX
\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  •  Open in writeLaTeX
\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.
User avatar
Juanjo
 
Posts: 656
Joined: Sat Jan 27th, 2007

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

Postby fractal.generator on Wed Oct 29th, 2008

Dear all,

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


regards,


Hayashi
fractal.generator
 
Posts: 66
Joined: Tue Oct 7th, 2008
Location: Japan


Return to General

Who is online

Users browsing this forum: Majestic-12 [Bot], Yahoo [Bot] and 21 guests