異なるタイプの変数を使った計算

異なるタイプの変数を使った計算

tikz 画像内のノードの高さを計算し、長さを別の長さで割ってカウンターを取得し、それを切り上げる方法はありますか? ある場合、異なるタイプの変数を使用した計算の主題の後でどこで読めますか?

次に例を示します。

\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}

答え1

あなたが何をするつもりなのか私にはわかりません。しかし、あなたのサブ質問については:

tikz画像内のノードの高さを計算する

に似ているtikzpicture 内の \widthof

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

ある長さを別の長さで割る

できます\usetikzlibrary{math}、そして単に

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

そして切り上げる

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

しかしご注意ください

  • 行数はこのように計算することはできない。TeXは行を分割する傾向があるため、適切な位置、行数は通常より多くなります。
  • 「線の高さ(合計)」は定数ではありません。たとえば{x xx}、、{M MM}およびは{j jj}高さが異なります。
  • さらに悪いことに、「線の高さ(全体)段落で' は、表示されるものと異なります\settototalheight。通常、前者は です\baselineskipが、それよりも大きくなることもあります。

だから次のコードがあなたのアプローチに近いと思います

\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}

関連情報