Text Formattingproblem with \renewcommand

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

problem with \renewcommand

Post by ghostanime2001 »

I don't understand why redefining \ce doesn't seem to work with \renewcommand.

Code: Select all

\documentclass{article} 
\usepackage[version=3]{mhchem}
\renewcommand{\ce}[2]{$\ce{#1_{(#2)}}$}
\begin{document}
\ce{H2O}{g}
\end{document}

Recommended reading 2024:

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

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

problem with \renewcommand

Post by Stefan Kottwitz »

You caused a loop by defining \ce with \ce itself. This works:

Code: Select all

\documentclass{article}
\usepackage[version=3]{mhchem}
\let\origce\ce
\renewcommand{\ce}[2]{$\origce{#1_{(#2)}}$}
\begin{document}
\ce{H2O}{g}
\end{document}
Stefan
LaTeX.org admin
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

problem with \renewcommand

Post by ghostanime2001 »

What does this do ?
\let\origce\ce
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: problem with \renewcommand

Post by Stefan Kottwitz »

It copies \ce to \origce. So you can use \origce to redefine \ce, without to cause a recursion.

Stefan
LaTeX.org admin
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: problem with \renewcommand

Post by ghostanime2001 »

It doesn't seem to work inside a tikzpicture
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: problem with \renewcommand

Post by Stefan Kottwitz »

Please elaborate, what you mean by it doesn't work. You did not tell before that you would like to use tikzpicture.

Stefan
LaTeX.org admin
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

Re: problem with \renewcommand

Post by ghostanime2001 »

inside a matrix of math nodes using tikz
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: problem with \renewcommand

Post by Stefan Kottwitz »

That's still quite theoretical. I expect that it works.

Stefan
LaTeX.org admin
ghostanime2001
Posts: 402
Joined: Fri May 20, 2011 9:41 am

problem with \renewcommand

Post by ghostanime2001 »

It gives me this error with this code using your definition of \ce

Code: Select all

\documentclass[fleqn]{article} 
\usepackage{amsmath}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix}
\let\origce\ce
\renewcommand{\ce}[2]{$\origce{#1_{(#2)}}$}
\begin{document}
\begin{tikzpicture}[baseline=(m-1-1.base)]
\matrix (m) [matrix of math nodes,ampersand replacement=\&]
{
\vphantom{.} \& \node {\ce{CO}{g}}; \& \node {}; \& \node {}; \\
\ce{} \& \node {}; \& \node {}; \& \node {}; \\
\Delta \& \node {}; \& \node }; \& \node {}; \\
\ce{} \& \node {}; \& \node {}; \& \node {}; \\
};
\end{tikzpicture}
\end{document}
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

problem with \renewcommand

Post by Stefan Kottwitz »

Your matrix already has math nodes, so you don't need nodes in the cells, and you don't need the math mode in the redefinition.

Code: Select all

\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage[version=3]{mhchem}
\usepackage{tikz}
\usetikzlibrary{matrix}
\let\origce\ce
\renewcommand{\ce}[2]{\origce{#1_{(#2)}}}
\begin{document}
\begin{tikzpicture}[baseline=(m-1-1.base)]
\matrix (m) [matrix of math nodes,ampersand replacement=\&]
{
\vphantom{.} \& \ce{CO}{g} \& {} \&  {} \\} ;
\end{tikzpicture}
\end{document}
Stefan
LaTeX.org admin
Post Reply