LaTeX forum ⇒ GeneralHow to print out the value of \textwidth?

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

How to print out the value of \textwidth?

Postby fractal.generator » Fri Oct 10, 2008 8:07 am

Dear all,

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

  1. \newcommand*{\MARKER}%
  2. {\noindent\strut\vrule
  3. \hrulefill~half textwidth =? cm~\hrulefill\vrule
  4. \hrulefill~half textwidth =? cm~\hrulefill\vrule
  5. \marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}


Please help me.


Thank you in advance.

regards,

Hayashi

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby Juanjo » Fri Oct 10, 2008 12:18 pm

  1. \the\textwidth
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

Link:
BBcode:
HTML:
Hide post links
Show post links

fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am
Location: Japan

Postby fractal.generator » Fri Oct 10, 2008 12:50 pm

Juanjo wrote:
  1. \the\textwidth


Mr. Juanjo,
Thanks for replying.

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

Thanks in advance.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7803
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Fri Oct 10, 2008 8:42 pm

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:
  1. \documentclass[a4paper,10pt]{article}
  2. \setlength{\textwidth}{14cm}
  3. \newlength\textwidthcm
  4. \textwidthcm=.03514598035146\textwidth
  5. \begin{document}
  6. {\catcode`p=12 \catcode`t=12 \gdef\cm#1pt{#1cm}}
  7. \begin{tabular}{ll}
  8. textwidth in pt: \the\textwidth \\
  9. textwidth in cm: \expandafter\cm\the\textwidthcm
  10. \end{tabular}
  11. \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
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby Juanjo » Sat Oct 11, 2008 1:14 am

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:
  1. \documentclass[a4paper]{article}
  2. \usepackage{fp}
  3.  
  4. \newlength{\TOarg} \newlength{\TOunit}
  5. {\catcode`p=12 \catcode`t=12 \gdef\TOnum#1pt{#1}}
  6. \newcommand\TOop[2]{\setlength{\TOarg}{#2}%
  7. \FPdiv\TOres{\expandafter\TOnum\the\TOarg}{\expandafter\TOnum\the\TOunit}%
  8. \FPround\TOres\TOres{#1}}
  9. \newcommand{\TOspace}{\ }
  10. \newcommand\TOpt[2][2]{\setlength{\TOunit}{1pt}\TOop{#1}{#2}\TOres\TOspace pt}
  11. \newcommand\TOin[2][2]{\setlength{\TOunit}{1in}\TOop{#1}{#2}\TOres\TOspace in}
  12. \newcommand\TOcm[2][2]{\setlength{\TOunit}{1cm}\TOop{#1}{#2}\TOres\TOspace cm}
  13. \newcommand\TOmm[2][2]{\setlength{\TOunit}{1mm}\TOop{#1}{#2}\TOres\TOspace mm}
  14. \newcommand\TOem[2][2]{\setlength{\TOunit}{1em}\TOop{#1}{#2}\TOres\TOspace em}
  15.  
  16. \begin{document}
  17. 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:
  18.  
  19. \begin{center}
  20. \renewcommand{\arraystretch}{1.5}
  21. \begin{tabular}{r*{4}{@{\ =\ }r}}
  22. 1pt & \TOpt[0]{1pt} & \TOin[3]{1pt} & \TOcm{1pt} & \TOmm{1pt} \\
  23. 1in & \TOpt{1in} & \TOin[0]{1in} & \TOcm{1in} & \TOmm[1]{1in} \\
  24. 1cm & \TOpt{1cm} & \TOin[3]{1cm} & \TOcm[0]{1cm} & \TOmm[0]{1cm} \\
  25. 1mm & \TOpt{1mm} & \TOin[3]{1mm} & \TOcm{1mm} & \TOmm[0]{1mm}
  26. \end{tabular}
  27. \end{center}
  28.  
  29. The em unit depends on which font is active:
  30. \begin{itemize}
  31. \item 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}
  32. \item {\bfseries 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
  33. \item {\bfseries \large 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
  34. \item {\ttfamily 1 cm = \TOem{1cm}, 1 em = \TOcm{1em}}
  35. \end{itemize}
  36. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Stefan Kottwitz
Site Admin
Posts: 7803
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Sat Oct 11, 2008 2:18 am

Hi Juanjo,

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

Stefan
Site admin

Link:
BBcode:
HTML:
Hide post links
Show post links

fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am
Location: Japan

Postby fractal.generator » Sat Oct 11, 2008 4:06 pm

To Stefen and Juanjo,

thanks for replying.


I am trying to diggest the codes :lol:



Hayashi

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby Juanjo » Mon Oct 13, 2008 12:41 am

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
  1. \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
  1. \makeatletter
  2. \newcommand\TOop[2]{\setlength{\TOarg}{#2}\FPdiv\TOres{\strip@pt\TOarg}{\strip@pt\TOunit}\FPround\TOres\TOres{#1}}
  3. \makeatother
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

Link:
BBcode:
HTML:
Hide post links
Show post links

fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am
Location: Japan

Postby fractal.generator » Wed Oct 29, 2008 6:25 pm

Dear all,

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


regards,


Hayashi

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “General”

Who is online

Users browsing this forum: Yahoo [Bot] and 8 guests