Adding width of text to node coordinate?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am currently trying to position some text in the center of a square drawn in a tikzpicture, like so:



example image



However, I am having a bit of trouble.



Take the following example:



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red] (boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight);

node at (textPosX, textPosY) someText;
endtikzpicture

enddocument


The problem arrises when I try using the width of the text: widthofsomeText/2 (from the calc package). (Reference Post)



According to the same reference post, widthof returns a length in points, which is incompatible with centimeters. Therefore, I tried converting to centimeters by using the method described here: printinunitsofcmwidthofsomeText.



However, this still yields an "Illegal unit of measure (pt inserted)."



What am I doing wrong and how can I correctly convert my units from points to centimeters?










share|improve this question









New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
    – Zarko
    36 mins ago






  • 1




    You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
    – marmot
    35 mins ago











  • @Zarko I added a picture to help convey what I want.
    – Dan Hoynoski
    29 mins ago














up vote
1
down vote

favorite












I am currently trying to position some text in the center of a square drawn in a tikzpicture, like so:



example image



However, I am having a bit of trouble.



Take the following example:



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red] (boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight);

node at (textPosX, textPosY) someText;
endtikzpicture

enddocument


The problem arrises when I try using the width of the text: widthofsomeText/2 (from the calc package). (Reference Post)



According to the same reference post, widthof returns a length in points, which is incompatible with centimeters. Therefore, I tried converting to centimeters by using the method described here: printinunitsofcmwidthofsomeText.



However, this still yields an "Illegal unit of measure (pt inserted)."



What am I doing wrong and how can I correctly convert my units from points to centimeters?










share|improve this question









New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
    – Zarko
    36 mins ago






  • 1




    You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
    – marmot
    35 mins ago











  • @Zarko I added a picture to help convey what I want.
    – Dan Hoynoski
    29 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am currently trying to position some text in the center of a square drawn in a tikzpicture, like so:



example image



However, I am having a bit of trouble.



Take the following example:



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red] (boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight);

node at (textPosX, textPosY) someText;
endtikzpicture

enddocument


The problem arrises when I try using the width of the text: widthofsomeText/2 (from the calc package). (Reference Post)



According to the same reference post, widthof returns a length in points, which is incompatible with centimeters. Therefore, I tried converting to centimeters by using the method described here: printinunitsofcmwidthofsomeText.



However, this still yields an "Illegal unit of measure (pt inserted)."



What am I doing wrong and how can I correctly convert my units from points to centimeters?










share|improve this question









New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am currently trying to position some text in the center of a square drawn in a tikzpicture, like so:



example image



However, I am having a bit of trouble.



Take the following example:



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red] (boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight);

node at (textPosX, textPosY) someText;
endtikzpicture

enddocument


The problem arrises when I try using the width of the text: widthofsomeText/2 (from the calc package). (Reference Post)



According to the same reference post, widthof returns a length in points, which is incompatible with centimeters. Therefore, I tried converting to centimeters by using the method described here: printinunitsofcmwidthofsomeText.



However, this still yields an "Illegal unit of measure (pt inserted)."



What am I doing wrong and how can I correctly convert my units from points to centimeters?







tikz-pgf positioning calculations unit-of-measure






share|improve this question









New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 29 mins ago





















New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 44 mins ago









Dan Hoynoski

1255




1255




New contributor




Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Dan Hoynoski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
    – Zarko
    36 mins ago






  • 1




    You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
    – marmot
    35 mins ago











  • @Zarko I added a picture to help convey what I want.
    – Dan Hoynoski
    29 mins ago
















  • can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
    – Zarko
    36 mins ago






  • 1




    You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
    – marmot
    35 mins ago











  • @Zarko I added a picture to help convey what I want.
    – Dan Hoynoski
    29 mins ago















can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
– Zarko
36 mins ago




can you provide a sketch of your desired result. is behind this tikzpicturesomething which require so complicated code?
– Zarko
36 mins ago




1




1




You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
– marmot
35 mins ago





You cannot do computations with newcommand, use pgfmathsetmacro instead, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. But this is absolutely unnecessary here, as far as I can see.
– marmot
35 mins ago













@Zarko I added a picture to help convey what I want.
– Dan Hoynoski
29 mins ago




@Zarko I added a picture to help convey what I want.
– Dan Hoynoski
29 mins ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










From your code I sort of sense what you might want:



documentclassarticle
usepackage[utf8]inputenc
usepackagetikzpagenodes
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
node[draw,line width=0.05cm, rounded corners=.3cm, color=red,
minimum width=boxWidth,minimum height=boxHeight,text=black] at
(boxPosX,boxPosY) someText;
endtikzpicture

enddocument


enter image description here



Instead of newcommand use pgfmathsetmacro to do computations, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. However, I think that all the complicated computations are unnecessary here. To put a text in the center of a box, it suffices to say node[draw]text. And then you can do all sorts of spirifankerln like adding a minimum width, rounded corners etc.






share|improve this answer




















  • Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
    – Dan Hoynoski
    26 mins ago










  • And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
    – Dan Hoynoski
    24 mins ago











  • @DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
    – marmot
    22 mins ago










  • you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
    – Dan Hoynoski
    8 mins ago






  • 1




    @DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
    – marmot
    4 mins ago

















up vote
3
down vote













If the text is to be placed in the centre of the rectangle, simply place it with a node midway down the path.



draw [line width=0.05cm, rounded corners=.3cm, color=red] 
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;


rectangle



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
%newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
%newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red]
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;

endtikzpicture

enddocument





share|improve this answer




















  • I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
    – Dan Hoynoski
    6 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Dan Hoynoski is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f454546%2fadding-width-of-text-to-node-coordinate%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










From your code I sort of sense what you might want:



documentclassarticle
usepackage[utf8]inputenc
usepackagetikzpagenodes
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
node[draw,line width=0.05cm, rounded corners=.3cm, color=red,
minimum width=boxWidth,minimum height=boxHeight,text=black] at
(boxPosX,boxPosY) someText;
endtikzpicture

enddocument


enter image description here



Instead of newcommand use pgfmathsetmacro to do computations, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. However, I think that all the complicated computations are unnecessary here. To put a text in the center of a box, it suffices to say node[draw]text. And then you can do all sorts of spirifankerln like adding a minimum width, rounded corners etc.






share|improve this answer




















  • Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
    – Dan Hoynoski
    26 mins ago










  • And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
    – Dan Hoynoski
    24 mins ago











  • @DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
    – marmot
    22 mins ago










  • you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
    – Dan Hoynoski
    8 mins ago






  • 1




    @DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
    – marmot
    4 mins ago














up vote
3
down vote



accepted










From your code I sort of sense what you might want:



documentclassarticle
usepackage[utf8]inputenc
usepackagetikzpagenodes
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
node[draw,line width=0.05cm, rounded corners=.3cm, color=red,
minimum width=boxWidth,minimum height=boxHeight,text=black] at
(boxPosX,boxPosY) someText;
endtikzpicture

enddocument


enter image description here



Instead of newcommand use pgfmathsetmacro to do computations, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. However, I think that all the complicated computations are unnecessary here. To put a text in the center of a box, it suffices to say node[draw]text. And then you can do all sorts of spirifankerln like adding a minimum width, rounded corners etc.






share|improve this answer




















  • Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
    – Dan Hoynoski
    26 mins ago










  • And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
    – Dan Hoynoski
    24 mins ago











  • @DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
    – marmot
    22 mins ago










  • you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
    – Dan Hoynoski
    8 mins ago






  • 1




    @DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
    – marmot
    4 mins ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted






From your code I sort of sense what you might want:



documentclassarticle
usepackage[utf8]inputenc
usepackagetikzpagenodes
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
node[draw,line width=0.05cm, rounded corners=.3cm, color=red,
minimum width=boxWidth,minimum height=boxHeight,text=black] at
(boxPosX,boxPosY) someText;
endtikzpicture

enddocument


enter image description here



Instead of newcommand use pgfmathsetmacro to do computations, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. However, I think that all the complicated computations are unnecessary here. To put a text in the center of a box, it suffices to say node[draw]text. And then you can do all sorts of spirifankerln like adding a minimum width, rounded corners etc.






share|improve this answer












From your code I sort of sense what you might want:



documentclassarticle
usepackage[utf8]inputenc
usepackagetikzpagenodes
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
node[draw,line width=0.05cm, rounded corners=.3cm, color=red,
minimum width=boxWidth,minimum height=boxHeight,text=black] at
(boxPosX,boxPosY) someText;
endtikzpicture

enddocument


enter image description here



Instead of newcommand use pgfmathsetmacro to do computations, e.g. pgfmathsetmacrotextPosXboxPosX + width("someText")*1pt/2cm;. However, I think that all the complicated computations are unnecessary here. To put a text in the center of a box, it suffices to say node[draw]text. And then you can do all sorts of spirifankerln like adding a minimum width, rounded corners etc.







share|improve this answer












share|improve this answer



share|improve this answer










answered 29 mins ago









marmot

64.1k470139




64.1k470139











  • Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
    – Dan Hoynoski
    26 mins ago










  • And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
    – Dan Hoynoski
    24 mins ago











  • @DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
    – marmot
    22 mins ago










  • you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
    – Dan Hoynoski
    8 mins ago






  • 1




    @DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
    – marmot
    4 mins ago
















  • Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
    – Dan Hoynoski
    26 mins ago










  • And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
    – Dan Hoynoski
    24 mins ago











  • @DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
    – marmot
    22 mins ago










  • you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
    – Dan Hoynoski
    8 mins ago






  • 1




    @DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
    – marmot
    4 mins ago















Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
– Dan Hoynoski
26 mins ago




Why can I do that addition in my new command for textPosY, but I cannot do more complex computation? Why does newcommand allows some computation?
– Dan Hoynoski
26 mins ago












And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
– Dan Hoynoski
24 mins ago





And I need something complex like this, because I will have several rectangles of the same size, each of which will have a unique string in them. The node method contours around the text only and adding redundant spaces is messy in my opinion.
– Dan Hoynoski
24 mins ago













@DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
– marmot
22 mins ago




@DanHoynoski newcommand by itself does not allow for computations. You're loading the calc package (which is not to be confused with the TikZ library of the same name), which allows you to do computations, but I guess once you load TikZ, this is sort of obsolete. And no, you can put pretty much anything inside a node of tikzpicture, graphics, equations... (just not tikzpictures, at least you shouldn't). There are tons of libraries which allow you to arrage rectangles of the same size, e.g. matrix or chains.
– marmot
22 mins ago












you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
– Dan Hoynoski
8 mins ago




you are absolutely correct! Using a node is so much better. And that is strange, because if you set textPosX to boxPosX in my original code above (to prevent that conversion error) and then it compiles just fine even with boxPosY + boxHeight / 2 being there. Are you say that because I am loading the calc package, it added this ability to newcommand?
– Dan Hoynoski
8 mins ago




1




1




@DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
– marmot
4 mins ago




@DanHoynoski No, calc does not do any magic of that kind. It is TikZ which starts to parse these things and then sees "Oh, here is some x+y, let's add them." You can always check such things by saying e.g. typeoutboxPosX and the you will see whether this is the string x+y or the sum.
– marmot
4 mins ago










up vote
3
down vote













If the text is to be placed in the centre of the rectangle, simply place it with a node midway down the path.



draw [line width=0.05cm, rounded corners=.3cm, color=red] 
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;


rectangle



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
%newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
%newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red]
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;

endtikzpicture

enddocument





share|improve this answer




















  • I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
    – Dan Hoynoski
    6 mins ago














up vote
3
down vote













If the text is to be placed in the centre of the rectangle, simply place it with a node midway down the path.



draw [line width=0.05cm, rounded corners=.3cm, color=red] 
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;


rectangle



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
%newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
%newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red]
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;

endtikzpicture

enddocument





share|improve this answer




















  • I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
    – Dan Hoynoski
    6 mins ago












up vote
3
down vote










up vote
3
down vote









If the text is to be placed in the centre of the rectangle, simply place it with a node midway down the path.



draw [line width=0.05cm, rounded corners=.3cm, color=red] 
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;


rectangle



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
%newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
%newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red]
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;

endtikzpicture

enddocument





share|improve this answer












If the text is to be placed in the centre of the rectangle, simply place it with a node midway down the path.



draw [line width=0.05cm, rounded corners=.3cm, color=red] 
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;


rectangle



documentclassarticle
usepackage[utf8]inputenc
usepackagegraphicx
usepackagetikzpagenodes
usepackagetikz
usepackagecalc
usepackagelayouts

begindocument

begintikzpicture[overlay, remember picture, x=1cm, y=1cm]
newcommandboxWidth5cm;
newcommandboxHeight3cm;
newcommandboxPosX5cm;
newcommandboxPosY-5cm;

newcommandsomeTextHello World;
%newcommandtextPosXboxPosX + (printinunitsofcmwidthofsomeText/2);
%newcommandtextPosYboxPosY + boxHeight / 2;

draw [line width=0.05cm, rounded corners=.3cm, color=red]
(boxPosX,boxPosY) rectangle ++(boxWidth, boxHeight)node[midway,text=black]someText;

endtikzpicture

enddocument






share|improve this answer












share|improve this answer



share|improve this answer










answered 19 mins ago









AndréC

3,566729




3,566729











  • I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
    – Dan Hoynoski
    6 mins ago
















  • I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
    – Dan Hoynoski
    6 mins ago















I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
– Dan Hoynoski
6 mins ago




I appreciate your answer! I did not think about adding another node like this! If I could accept two answers, I would!
– Dan Hoynoski
6 mins ago










Dan Hoynoski is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















Dan Hoynoski is a new contributor. Be nice, and check out our Code of Conduct.












Dan Hoynoski is a new contributor. Be nice, and check out our Code of Conduct.











Dan Hoynoski is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f454546%2fadding-width-of-text-to-node-coordinate%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery