涉及 TikZ 圖片的宏後有空格

涉及 TikZ 圖片的宏後有空格

我編寫了一個巨集來使用 TikZ 列印線性組合和相關內容(pgffor確實如此,但我需要 TikZ 來處理大文件中的其他內容)。這是一個 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}

輸出在最後。我想要的是其中兩個之間有適當的間距。正如您所看到的,我什至嘗試將代碼用作獨立的文本,但得到了相同的結果;我也嘗試過使用tikzpicture環境,但沒有任何變化。搜尋後我看到很多問題消除a 後面有空格tikzpicture,但我想加它。正如您所看到的,xspace這裡也不起作用。提前致謝!

在此輸入影像描述

答案1

您不需要將命令放入tikz或中tikzpicture,因為代碼只是文本,而不是繪圖命令。事實上,TeX 確實正在嘗試排版這些字母,但由於它位於 a 的中間tikzpicture,因此它被指示使用nullfont,這是一種內部字體,根本沒有字符,也不會產生字間空格。因此\tikztikzpicture環境只需刪除文本,因為它不在節點中。

\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}

相關內容