1

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

Robert
  • 13
  • For point 2/ Your issue seems to be due to "hidden" spaces (i.e. end of lines) in your macro definition. You should either wrap it, or make extensive use of % – ebosi Nov 30 '16 at 15:20
  • Your \dashedTricommand starts with a space: just after the opening brace: So use {% there. And there is another one after \end{tikzpicture}. Put a % also there. – Pieter van Oostrum Nov 30 '16 at 15:28

1 Answers1

3

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.)

ebosi
  • 11,692
  • Thanks a lot for the help! I didn't know, that blank spaces are that crucial :) Cheers – Robert Nov 30 '16 at 19:01
  • Regarding issue #1, it's not solved in my code. If I use the marker "o", i get a perfectly centered circle. If I use "triangle" or "diamond", it is horizontally shiften. Do you know of other solutions? Since it works on your code ... Cheers – Robert Nov 30 '16 at 19:24
  • @Robert check if you have a % after the first curly bracket of your \node command. You might have another hidden space. – ebosi Nov 30 '16 at 19:34
  • Thanks for your answer, I checked my code (basically copy & pasted yours) and added a % to each line. The issue persists. However only for the triangle and diamond mark. For centering those I have to use 1mm and 0.5mm. Am I missing something else, since the positioning at (x,y) needs to be set individually in my case? – Robert Dec 01 '16 at 09:01
  • Indeed, point 1 is not addressed. Have a look at Why does \pgfuseplotmark put so much space to the left of the mark?. But if you run my MWE, you must get horizontally centered triangle/diamond (i.e. the same output as in the picture). Is it the case for you? If yes, then the error lies in your real code - and it's difficult to tell where if I don't see it. If not, then try to update your TeX distro. – ebosi Dec 01 '16 at 09:44
  • I'm running your exact code and do get a different result. I'll try to update my TeX distribution, as you suggested. Thanks for the comment :) – Robert Dec 01 '16 at 12:39
  • @Robert it's weird indeed since processing(input) -> output. If we have the same input, the only reason for a different output is a different processing. – ebosi Dec 01 '16 at 13:35
  • 1
    Instead of defining a (base) coordinate you can also just use baseline=-.5ex. – Emma Dec 03 '16 at 16:52