1

Is there any way to calculate the height of a node in a tikz pictures and to divide a length by an another length to get a counter and to round it up? If yes where could i read after the subject of calculating with different types of variables?

Here is an example:

\documentclass [a4paper] {article}  
\usepackage{calc}  
\usepackage{tikz,pgf}  
\usetikzlibary{arrows}  

%in this  case i want to do a framebox with the parameter of 1 line's length in a tikz pictures
%because i dont know how could i get the heigth of the node i try to calculate it by divide the full length of the text (what i got with the command \settototalheight from the calc package) by the length of 1 line (this is a parameter) so i got the number of lines(this is a value not a length so i cannot use the \divide command because the type of the variable)  
\newlength \widthofone \fullwidth \heightoneline \fullheight
\nemcounters \numberoflines 

\newcommand {\altframe}[2] { %#1 the text #2 the width of the lines
\widthofone=#2pt %im not sure how to convert a length from a value but i try to input the 2nd parameter into a length variable  
\settototalheight{\heightoneline}{qwe} %i get the height of one line by the calc package  
\setwidth{\totalwidth}{#1} %i calculate the width of the text    
%And now i would divide \fullwidth by \widthofone ,if i would know how to do it, to get  \numberoflines  
 %I would multiply it by \height to get \fullheight  
\begin{tikzpictures}  

   \node[text width=#2pt] at (0,0) {#1};%i write out the text  
   \draw[->,color=black] (\fullwidth/2,\fullheight/2) -- (0-\fullwidth/2,\fullheight/2); %i draw out the lines (i use height/2 becouse of the fact that the (0,0) coordinate is the middle of the picture)  
   \draw[->,color=black] (\fullwidth/2,0-\fullheight/2) -- (0-\fullwidth/2,0-\fullheight/2);  
   \draw[->,color=black] (\fullwidth/2,\fullheight/2) -- (\fullwidth/2,0-\fullheight/2);  
   \draw[->,color=black] (0-\fullwidth/2,\fullheight/2) -- (0-\fullwidth/2,0-\fullheight/2);   
\end{tikzpictures}
}

\begin{document}
\altframe{i love this sample text}{100}


\end{document}
  • Surely there is... How about providing the community with some example code (starting with \documentclass and ending with \end{document}) that highlights what you're after? – Werner Dec 23 '14 at 15:15
  • You can look at Math Library : section 56 of the PGF/TikZ manual (3.0.0). You can divide two lengths, round the result and put it in a counter. – Kpym Dec 23 '14 at 15:57
  • I'am not sure but in the 56.3 subsection it said that only the integrers, real numbers, and coordinates can be recognized by the package not lengths. – Adam Schweitzer Dec 23 '14 at 17:45
  • Have you tried ? Check this \tikzmath{\x=1cm/1mm;}\x for example. You can read about the units conversion in the section 89 Mathematical expressions. And by the way, Math Library is not the only way to do this, but is one possible solution among many others. – Kpym Dec 24 '14 at 8:16
  • Is your example what you want to do, or is just one "baby model" where you may need this kind of calculations ? If you want to solve this concrete example, you don't need all this calculations. You can use the node anchors to draw over the node. Here is one example : tex.stackexchange.com/a/218486/9335 – Kpym Dec 24 '14 at 8:28
1

I cannot figure out what are you going to do. But for your subquestions:

calculate the height of a node in a tikz pictures

Similar to \widthof within tikzpicture

\makeatletter
\newcommand\getheightofnode[2]{%
    \pgfextracty{#1}{\pgfpointanchor{#2}{north}}%
    \pgfextracty{\pgf@ya}{\pgfpointanchor{#2}{south}}%
    \addtolength{#1}{-\pgf@ya}}
\makeatother

divide a length by another length

you can \usetikzlibrary{math}, and simply write

\tikzmath{
    \numberoflines=\fullwidth/\widthofone;}

and round it up

\tikzmath{
    \numberoflines=ceil(\fullwidth/\widthofone);}

However, please notice that

  • the number of lines cannot be calculated like that. Since TeX tends to break lines at appropriate positions, the number of lines is usually larger.
  • 'the (total) height of a line' is not a constant. For example {x xx}, {M MM}, and {j jj} are of different height.
  • And still worse, 'the (total) height of a line in a paragraph' is different from what \settototalheight gives you. Usually the former is \baselineskip, but it can be larger.

So I guess the following code is closer to your approach

\documentclass{article}  
\usepackage{calc,tikz}\usetikzlibrary{math}

\newlength\widthofone \newlength\fullwidth \newlength\heightoneline \newlength\fullheight

\makeatletter
\newcommand\getheightofnode[2]{%
    \pgfextracty{#1}{\pgfpointanchor{#2}{north}}%
    \pgfextracty{\pgf@ya}{\pgfpointanchor{#2}{south}}%
    \addtolength{#1}{-\pgf@ya}}
\makeatother

\newcommand {\altframe}[2]{
\widthofone=#2
\heightoneline\baselineskip
\settowidth{\fullwidth}{#1}

\begin{tikzpicture}[->,color=black]
    \node[text width=#2](baby)at(0,0){#1};
    \getheightofnode\fullheight{baby}
    \tikzmath{
        \numberoflines=round(\fullheight/\heightoneline);}
    \draw(\fullwidth/2,\fullheight/2)--(-\fullwidth/2,\fullheight/2);
    \draw(\fullwidth/2,-\fullheight/2)--(-\fullwidth/2,-\fullheight/2);
    \draw(\fullwidth/2,\fullheight/2)--(\fullwidth/2,-\fullheight/2);
    \draw(-\fullwidth/2,\fullheight/2)--(-\fullwidth/2,-\fullheight/2);
    \node[text width=5cm]at(0,-3){
        widthofone      =\the\widthofone    \\
        fullwidth       =\the\fullwidth     \\
        heightoneline   =\the\heightoneline \\
        fullheight      =\the\fullheight    \\
        numberoflines   =    \numberoflines};
    \end{tikzpicture}}
\begin{document}
\altframe{i love this sample text}{50pt}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.