GeneralText width, height variable manipulation

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
triop
Posts: 5
Joined: Sat Mar 19, 2016 11:15 am

Text width, height variable manipulation

Post by triop »

Hello, I would like to get properties of typeset object. For example width and height of particular formatted text string. I would like to assign height of this string to variable and than use and manipulate with this variable in subsequent typesetting.

Say I have string "my string". I than assign width to variable mvar1=width("my string"). Than I would like to use mvar1 in formatting another object say "another string" like this: width("another object")=mvar1*0.5.

Is it somehow possible in TeX or LaTeX? I wouldn't hope for exact solution. Just the name of book or webpage which to read would suffice. Any other advice will be wellcome.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Text width, height variable manipulation

Post by Johannes_B »

Depending on what exactly you want, you can do something like this.

Code: Select all

\documentclass{article}
\begin{document}
\newlength{\stringlngth}
\settowidth{\stringlngth}{Wally Wombat}
Wally Wombat\par
\rule{\stringlngth}{10pt}

\settowidth{\stringlngth}{This is a speacial test}
This is a speacial test\par
\rule{\stringlngth}{10pt}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
triop
Posts: 5
Joined: Sat Mar 19, 2016 11:15 am

Text width, height variable manipulation

Post by triop »

Nice, that's what I hopped for. Thank you very much.

Say I have many thousands of such strings which I need to measure. I can generate code in R project into txt file like following:

Code: Select all

\settowidth{\stringlngth1}{string1}
\settowidth{\stringlngth2}{string2}
\settowidth{\stringlngthn}{stringn}
Can I use some mathematics on these variables? Like for example max(...), min(...), mean(...)? How would I do it please?

Is it possible to sink thoose values into txt file for example in following format?
stringlngth1=100px
stringlngth2=150px
stringlngthn=250px

Can I measure other objects like minipage, table cell or graphic elements?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Text width, height variable manipulation

Post by Johannes_B »

triop wrote:Can I measure other objects like minipage, table cell or graphic elements?
Yes and no. Probably. I have no idea what you are trying to achieve in the end.

Maybe start with explaining your goal, there might be a better way.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
triop
Posts: 5
Joined: Sat Mar 19, 2016 11:15 am

Text width, height variable manipulation

Post by triop »

For my purpose sinking variables with their values into file would suffice. Anything else I can do with R project. It would serve many purposes I can't possibly list them all.

For example I can dynamically set tabular data in pretty table using \longtable. Some things still bothers me. I have attached my table for illustration.

Image
Image


1a) I have to "manually" sellect columns which I want to print on one page and size of text in column headers and rows. This is not usually problem but I want also fully automatic table printing of long and wide tables. It seems that Longtable package can print only a long table but not a wide table. Wide table is typset outside page area. Any sollution?

1b) Some column headers and also cells with my data may have long strings of more than 20 words others are very short. I have written some algorithms in R which breaks each string into parts and than I wrap the parts like this \thead{It is\\ a good \\ day sir} using package \makecell. The problem is, I can't easily predict width and height for all strings when each string is typeset with different font and size.

What I would like to do is rather complex:
For example I have multicolumn with 3 columns, main header and 3 subheaders. I want to minimize white spaces in columns so that maximum of columns can fit to page.

I would like to set it with following conditions:
2a) width of the subcolumns is computed such that for example each cell has no more than 4 rows nor has greater width than the longest word in whole column except heading. No orphan short strings allowed.

2b) with 2a) I have conditions for minimal subcolumns widths. With these I can format column headding and subheaddings. Set fontsize and rotate them according an algorithm for example:
-font size is not bellow 10
-max. rows 3
-if sum of subcolumns widths is lover than width of heading give space to subcolumns equally. (Using multicolumn all space goes to last subcolumn.)
-if it results in to much space in any subcolumn try to break strings in columns with different parameters.

If I can sink size variables in txt I can possibly achieve everything described and much more.

Thank you very much dear reader to read it this far. Any advice is welcome.
triop
Posts: 5
Joined: Sat Mar 19, 2016 11:15 am

Text width, height variable manipulation

Post by triop »

With following the value of variable can be printed to pdf, so it should be somehow possible to write this value to txt file.

Code: Select all

\documentclass{article}
% \usepackage[verbose]{geometry}
\begin{document}
% \SweaveOpts{concordance=TRUE}
\newlength{\mylength}
\settowidth{\mylength}{some text}
\the\mylength
\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Text width, height variable manipulation

Post by Johannes_B »

Code: Select all

\documentclass{article}
\usepackage{listings}
	% \usepackage[verbose]{geometry}
\begin{document}
	    % \SweaveOpts{concordance=TRUE}
\newlength{\mylength}
\settowidth{\mylength}{some text}
\newwrite\triopSavedLengths
\immediate\openout\triopSavedLengths=triopSavedLengths.txt\relax
\immediate\write\triopSavedLengths{\the\mylength}
\immediate\closeout\triopSavedLengths

\lstinputlisting{triopSavedLengths.txt}
\end{document}
You might also want to look at LuaLaTeX. LuaTeX might have something for that purpose, i am not sure.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply