Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
ghostanime2001
Posts: 402 Joined: Fri May 20, 2011 9:41 am
Post
by ghostanime2001 » Mon Dec 19, 2011 8:01 am
How do you fake the width of one expression be the width of some other expression?
For example in this document I want the width of the first box to match that of the second box so how can one do it?
Code: Select all
\documentclass{article}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,nodes=draw]
{
& \node (a) {\text{what is a local ghost?}}; \\
& \node (b) {\text{LaTeX community}}; \\
};
\draw (a.north east) -- (b.south east);
\end{tikzpicture}
\end{document}
Last edited by
ghostanime2001 on Thu Dec 22, 2011 11:56 pm, edited 4 times in total.
NEW: TikZ book now 40% off at Amazon.com for a short time.
Stefan Kottwitz
Site Admin
Posts: 10345 Joined: Mon Mar 10, 2008 9:44 pm
Post
by Stefan Kottwitz » Mon Dec 19, 2011 9:26 am
It can be done by \makebox and \settowidth, or quick and easy by \rlap and \hphantom:
Code: Select all
\node (b) {\rlap{short text}\hphantom{long text}};
Stefan
LaTeX.org admin
ghostanime2001
Posts: 402 Joined: Fri May 20, 2011 9:41 am
Post
by ghostanime2001 » Mon Dec 19, 2011 11:01 am
There is another problem with using rlap and hphantom. The third row has lost the original centered alignment it once had. I know the MWE is long but I want to emphasize the important point. The important point is that the "0.05 - x" became left aligned and the first "x" in the 3rd row, 3rd column has also become left-aligned. I was hoping that they would retain the original position in the unedited version and only the widths would be expanded so that I could better draw the vertical lines. How can I fix this ? This table is near complete however.
Code: Select all
\documentclass{article}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\noindent With borders and unedited:\begin{tikzpicture}
\matrix [matrix of math nodes,nodes=draw]
{
& \node (a) {\rlap{\ce{H2S <=>}}\hphantom{\ce{H2S <=>}}}; & \node (b) {\ce{H+ +}}; & \node (c) {\ce{HS-}}; \\ \hline
\node (y) {\ce{S}}; & \node (d) {0.05}; & \node (e) {0}; & \node (f) {0}; \\
\node (z) {\ce{\Delta}}; & \node (g) {-x}; & \node (i) {x}; & \node (j) {x}; \\
\node (aa) {\ce{E}}; & \node (k1) {0.05-x}; & \node (l) {x}; & \node (m) {x}; \\
};
\draw (a.north west) -- (k1.south west);
\draw (a.north east) -- (k1.south east);
\draw (b.north east) -- (l.south east);
\end{tikzpicture} \\ \\
Without borders and unedited:\begin{tikzpicture}
\matrix [matrix of math nodes]
{
& \node (a) {\rlap{\ce{H2S <=>}}\hphantom{\ce{H2S <=>}}}; & \node (b) {\ce{H+ +}}; & \node (c) {\ce{HS-}}; \\ \hline
\node (y) {\ce{S}}; & \node (d) {0.05}; & \node (e) {0}; & \node (f) {0}; \\
\node (z) {\ce{\Delta}}; & \node (g) {-x}; & \node (i) {x}; & \node (j) {x}; \\
\node (aa) {\ce{E}}; & \node (k1) {0.05-x}; & \node (l) {x}; & \node (m) {x}; \\
};
\draw (a.north west) -- (k1.south west);
\draw (a.north east) -- (k1.south east);
\draw (b.north east) -- (l.south east);
\end{tikzpicture} \\ \\
With borders and edited (3rd row): \begin{tikzpicture}
\matrix [matrix of math nodes,nodes=draw]
{
& \node (a) {\rlap{\ce{H2S <=>}}\hphantom{\ce{H2S <=>}}}; & \node (b) {\ce{H+ +}}; & \node (c) {\ce{HS-}}; \\ \hline
\node (y) {\ce{S}}; & \node (d) {0.05}; & \node (e) {0}; & \node (f) {0}; \\
\node (z) {\ce{\Delta}}; & \node (g) {-x}; & \node (i) {x}; & \node (j) {x}; \\
\node (aa) {\ce{E}}; & \node (k1) {\smash[b]{\rlap{$0.05-x$}}\hphantom{\ce{H2S <=>}}}; & \node (l) {\smash[b]{\rlap{$x$}}\hphantom{\ce{H+ +}}}; & \node (m) {x}; \\
};
\draw (a.north west) -- (k1.south west);
\draw (a.north east) -- (k1.south east);
\draw (b.north east) -- (l.south east);
\end{tikzpicture} \\ \\
Without borders and edited (3rd row): \begin{tikzpicture}
\matrix [matrix of math nodes]
{
& \node (a) {\rlap{\ce{H2S <=>}}\hphantom{\ce{H2S <=>}}}; & \node (b) {\ce{H+ +}}; & \node (c) {\ce{HS-}}; \\ \hline
\node (y) {\ce{S}}; & \node (d) {0.05}; & \node (e) {0}; & \node (f) {0}; \\
\node (z) {\ce{\Delta}}; & \node (g) {-x}; & \node (i) {x}; & \node (j) {x}; \\
\node (aa) {\ce{E}}; & \node (k1) {\smash[b]{\rlap{$0.05-x$}}\hphantom{\ce{H2S <=>}}}; & \node (l) {\smash[b]{\rlap{$x$}}\hphantom{\ce{H+ +}}}; & \node (m) {x}; \\
};
\draw (a.north west) -- (k1.south west);
\draw (a.north east) -- (k1.south east);
\draw (b.north east) -- (l.south east);
\end{tikzpicture} \\ \\
\end{document}
cgnieder
Site Moderator
Posts: 2000 Joined: Sat Apr 16, 2011 7:27 pm
Post
by cgnieder » Mon Dec 19, 2011 12:51 pm
Maybe the tikz keys "text width", "minimum width" and "align" would help?
Code: Select all
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,nodes=draw,text width=6cm,align=left]
{
& \node (a) {\text{a text that is longer than the other}}; \\
& \node (b) {\text{short text}}; \\
& \node (c) {\text{third row in the middle}}; \\
};
\draw (a.north east) -- (c.south east);
\end{tikzpicture}
\end{document}
ghostanime2001
Posts: 402 Joined: Fri May 20, 2011 9:41 am
Post
by ghostanime2001 » Mon Dec 19, 2011 10:26 pm
Maybe one box has to be created inside another box to have it aligned in the center? I think using \rlap and \hphantom did help but maybe this box needs to be placed inside another box ? BTW using align and text width did not help.
ghostanime2001
Posts: 402 Joined: Fri May 20, 2011 9:41 am
Post
by ghostanime2001 » Mon Dec 19, 2011 11:32 pm
This is what you call almost perfect solution:
Code: Select all
\documentclass{article}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usepackage{makebox}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes]
{
& \node (a) {\ce{H2S <=>}}; & \node (b) {\ce{H+ +}}; & \node (c) {\ce{HS-}}; \\ \hline
\ce{S} & \node {0.05}; & \node {0}; & \node {0}; \\
\Delta & \node {-x}; & \node {+x}; & \node {+x}; \\
\ce{E} & \node (d) {\smash[b]{\makebox*{\ce{H2S <=>}}{$0.05-x$}}}; & \node (e) {\smash[b]{\makebox*{\ce{H+ +}}{$x$}}}; & \node (f) {\smash[b]{x}}; \\
};
\draw (a.north west) -- (d.south west);
\draw (a.north east) -- (d.south east);
\draw (b.north east) -- (e.south east);
\end{tikzpicture}
\end{document}