0

I wrote a macro to print linear combinations and related things using TikZ (pgffor really, but I need TikZ for other things in a big document). Here's a MWE:

\documentclass[11pt]{article}

\usepackage{tikz,spalign,xspace,amsmath}

\newcommand\lincomb[3]{% 3args: list of coeffs, vector symb, operation
%   \begin{tikzpicture}
    \tikz{
        \foreach \x [count=\j] in {#1}{
            \ifnum\j=1
                $\x{#2}_{\j}$
            \else
                ${#3}\x{#2}_{\j}$
            \fi
        }\xspace
%   \end{tikzpicture}
    }
}


\begin{document}

\begin{equation}
    \spalignmat{0.7;0.5;0.1}={\lincomb{0.7,0.5,0.1}{\mathbf{e}}{+}}\quad
    \;\text{and}\:
    \spalignmat{0.1;0.5;0.7}=\lincomb{0.1,0.5,0.7}{\mathbf{e}}{+}
\end{equation}

$\lincomb{a,b,c}{f}{-}$ and then some text.

\begin{tikzpicture}
    \foreach \x [count=\j] in {a,b,c,d}{
        \ifnum\j=1
            $\x f_{\j}$
        \else
            $+ \x f_{\j}$
        \fi
    }
\end{tikzpicture}\ and then some text

\end{document}

The output is at the end. What I want is to have proper spacing between two of these. As you can see, I even tried using the code as a standalone piece of text, but got the same result; I also tried using the tikzpicture environment, but nothing changes. After seraching I've seen many questions asking to remove space after a tikzpicture, but I want to add it. As you can see also, xspace doesn't work here either. Thanks in advance!!

enter image description here

3

You don't need put the commands in tikz or tikzpicture, as the code is just a text, not drawing commands. In fact TeX is indeed trying to typeset these letters, but since it is in the middle of a tikzpicture, it has been instructed to use the nullfont, which is an internal font that has no character at all and produces no interword space either. So \tikz or tikzpicture environment just drop the text as it is not in a node.

\documentclass[11pt]{article}

\usepackage{tikz,spalign,amsmath}

\newcommand\lincomb[3]{% 3args: list of coeffs, vector symb, operation
%   \begin{tikzpicture}
%    \tikz{
        \foreach \x [count=\j] in {#1}{
            \ifnum\j=1
               \x{#2}_{\j}
            \else
               {#3}\x{#2}_{\j}
            \fi
%        \xspace
%   \end{tikzpicture}
        }
}

\begin{document}

\begin{equation}
  \spalignmat{0.7;0.5;0.1}={\lincomb{0.7,0.5,0.1}{\mathbf{e}}{+}}\quad
    \;\text{and}\:
  \spalignmat{0.1;0.5;0.7}=\lincomb{0.1,0.5,0.7}{\mathbf{e}}{+}
\end{equation}

$\lincomb{a,b,c}{f}{+}$ and then some text.

\end{document}
  • In my opinion the \ensuremath should be dropped as well. It has the potential to disrupt the spacing and it doesn't really serve any purpose since this macro is only going to be used inside math mode anyway. – Henri Menke Sep 14 '20 at 0:14
  • This works! Thanks! As @HenriMenke says, it doesn't need the \ensuremath macro. Any idea of why this happens? I put an \fbox befoe the \tikz command and it drew a very small box around the lower left corner of the first character; so it may have something to do with the bounding box, but I cannot figure out what. – Óscar Guajardo Sep 14 '20 at 0:16
  • @HenriMenke Yes, just for testing, I forgot removing it, thank you. – Amita Sep 14 '20 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.