Graphics, Figures & TablesTikZ: How can I add two pieces of text?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I add two pieces of text?

Post by topsquark »

Sorry about the thread title but I've learned that I no longer know the technical term. It used to be called "concatenation of strings." But strings are now something completely different.

Anyway, what I'm up to is to tack a number onto a piece of text. Normally this would be easy. But the text is "/2". Any time I put a number on the front to make a fraction it's no longer a piece of text, it's a fraction and it gets evaluated. I'm working on a quick routine to take the number 0.5 and change it to the text "1/2". And similarly 1.5 to "3/2". But if I run 1 and /2 together I just get 0.5 again.

I can do this by using nested if statements if I have to but there ought to be a way to do this. I suspect that part of my problem in looking up a solution is that I don't know what to call this process anymore!

Thanks!

-Dan

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

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

TikZ: How can I add two pieces of text?

Post by Bartman »

Please add a Infominimal working example marked with code tags to your explanation, so that one can see why your input is evaluated.
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I add two pieces of text?

Post by topsquark »

Bartman wrote:Please add a Infominimal working example marked with code tags to your explanation, so that one can see why your input is evaluated.
I didn't include the code because it was simply \b = 0.5; \let \a = "\b/2"; and various other attempts.

Anyway, you're right, I should have included it. My apologies.

I did finally find an answer.

Code: Select all

\begin{tikzpicture}
     \tikzmath {
          int \b;
          function f(\n) {
               if int(\n)<\n then {\b=2*\n; let \r="\b/2"; }
                    else {let \r=1; };
               return \r;
          };
          \p = 0.5; \a = f(\p);
          { \node at (0,0) {\a}; };
     };
\end{tikzpicture}
(I used a function code because that's how I'm doing it in my main program.)

I appreciate the response.

-Dan

Addendum: It works on my system. I have no idea why it isn't working here. Sorry about that!
Post Reply