Text FormattingFaking Height of one Expression with another

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Faking Height of one Expression with another

Post by ghostanime2001 »

I know there is a command to fake the width of one expression with another expression using the \makebox* command from "makebox" package. But is there also another command to fake the height of one expression with another expression ?
Last edited by ghostanime2001 on Wed Dec 21, 2011 1:15 am, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Faking Height of one Expression with another

Post by 5gon12eder »

You may use the plain TeX macros \smash and \vphantom to achieve this. In the following example, in the first paragraph, the height of the huge word ``line'' is smashed to zero. In the second paragraph, the second line consumes the same height as if the word ``line'' would be there in \huge font.

Code: Select all

\documentclass{article}

\begin{document}
  \noindent%
  This is a line of text.\\
  This is a \smash{\huge line} of text.\\
  This is a line of text.
  
  \vspace{5ex}
  
  \noindent%
  This is a line of text.\\
  This is a\vphantom{\huge line} of text.\\
  This is a line of text.

\end{document}
demo.png
demo.png (12.83 KiB) Viewed 6800 times
Equipped with this, you can define the macro \fakeheight that typesets its first argument as if it had the height from the second.

Code: Select all

\newcommand{\fakeheight}[2]{\mbox{\smash{#1}\vphantom{#2}}}
The whole story is very nicely described in a TGUBoat article by Alexander R. Perlis.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Faking Height of one Expression with another

Post by ghostanime2001 »

When I try to use the \fakeheight command on chemical formula's the positioning of the superscript (+) charge moves upwards from the original positioning in the original table. How can I prevent this from happening ?

Code: Select all

    \documentclass{article}
    \usepackage[version=3]{mhchem}
    \usepackage{tikz}
    \usepackage{makebox}
    \usepackage{nccboxes}
    \usetikzlibrary{matrix,calc}
\newcommand{\fk}[2]{\mbox{\smash[t]{#1}\vphantom{#2}}}
\begin{document}
Original: \begin{tikzpicture}
\matrix (m) [matrix of math nodes,ampersand replacement=\&,nodes=draw]
{
\vphantom{\ce{H2S <=>}} \& \node (a) {\ce{H2S <=>}}; \& \node (b) {\ce{H+ +}}; \& \node (c) {\ce{HS-}}; \\ \hline
a \& \node {b}; \& \node {c}; \& \node {d}; \\
a \& \node {b}; \& \node {c}; \& \node {d}; \\
a \& \node {b}; \& \node {c}; \& \node {d}; \\
};
\end{tikzpicture} \\ \\ 
Now look at the negative charge on \ce{H+} and \ce{HS-} \\
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,ampersand replacement=\&,nodes=draw]
{
\& \node (a) {\fk{\ce{H2S <=>}}{\ce{HS-}}}; \& \node (b) {\fk{\ce{H+ +}}{\ce{HS-}}}; \& \node (c) {\fk{\ce{HS-}}{\ce{HS-}}}; \\ \hline
a \& \node {b}; \& \node {c}; \& \node {d}; \\
a \& \node {b}; \& \node {c}; \& \node {d}; \\
a \& \node {b}; \& \node {c}; \& \node {d}; \\
};
\end{tikzpicture}
\end{document}
5gon12eder
Posts: 126
Joined: Sun Feb 13, 2011 8:36 pm

Faking Height of one Expression with another

Post by 5gon12eder »

ghostanime2001 wrote:How can I prevent this from happening ?
Don't fake a different height but let TeX' positioning algorithms know the actual dimensions of the objects to arrange.
I'm using pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian).
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: Faking Height of one Expression with another

Post by ghostanime2001 »

There's nothing I can do ???
Post Reply