[Tex/LaTex] Nicely embedding tikzpicture in text

spacingtext;tikz-pgf

I have the following problem with tikzpicture. I've added a plot in my text, which has different line styles and markers inside. For better referencing of single curves, I decided to build the line and marker styles with tikzpicture, e.g. a dashed line with a triangle:

\newcommand{\dashedTri}{ 
            \begin{tikzpicture}
                \draw[black,dashed,line width=1.5pt](0,0) -- (5mm,0); 
                    \node[very thick, mark size=3pt,color=black] at (1mm,0){ 
                        \pgfuseplotmark{triangle} 
                    };
            \end{tikzpicture}
}

Which creates:

enter image description here

Note: I also created a diamond marker with a dashed line, by replacing triangle with diamond in the upper code snippet.

Here I have two problems:

  1. To have the triangle appear in the middle of the line, I had to put at (1mm,0) instead of (2.5mm,0). When using a circle (i.e. o), putting at (2.5mm,0) was okay for centering. Does anyone know, why I need different positioning for different markers?
  2. As indicaded by the (later added) red squares in the text: when using these tikzpictures the spacing in the text looks strange, as if the box has a too large width. Adjusting the width (via resizebox) hasn't solved the problem. Does anyone know, how to "nicely" embed the tikzpicture in the text?

Cheers,
Robert

Best Answer

Here is a solution that adresses both of your issues:

enter image description here

Here is how I address your issues:

  1. Vertical alignment: I defined a common point (named (base) at (0,-.5em)) in both of your TikZ picture. And I defined it as the reference for the baseline (cf. the global option baseline=(base)]% for each tikzpicture). It means this particular point will be vertically aligned with your text baseline.

  2. Horizontal blankspace: In your macro definition, the first line ends with space character (). It means that if you wrap lines 1 and 2, your code will look like this: \newcommand{\dashedTri}{ \begin{tikzpicture}; i.e. your embedding a space in your macro definition that is added on top of the space befor your command in your .tex document.


\documentclass{scrartcl}
    \usepackage{tikz}
    \usetikzlibrary{plotmarks}

    \newcommand{\dashedTri}{%
        \begin{tikzpicture}[inner sep=0pt, baseline=(base)]%
        \draw[black,dashed,line width=1.5pt](0,0) -- (5mm,0); 
        \node[very thick, mark size=3pt,color=black] at (2.5mm,0){% 
            \pgfuseplotmark{triangle}%
        };
        \node (base) at (0,-.5ex) {};
        \end{tikzpicture}%
    }

    \newcommand{\dashedDia}{%
        \begin{tikzpicture}[inner sep=0pt,baseline=(base)]%
        \draw[black,dashed,line width=1.5pt](0,0) -- (5mm,0); 
        \node[very thick, mark size=3pt,color=black] at (2.5mm,0){% 
            \pgfuseplotmark{diamond}%
        };
        \node (base) at (0,-.5ex) {};
        \end{tikzpicture}%
    }
\begin{document}
    Foo \dashedTri{} bar \dashedDia{} baz.
\end{document}

(Note that I changed in your code \node[very thick, mark size=3pt,color=black] at (1mm,0){ into \node[very thick, mark size=3pt,color=black] at (2.5mm,0){ (i.e. 1mm into 2.5mm) so that the plotmark is horizontally aligned regarding the dashed line.)

Related Question