I'm using the library tikzmark with usetikzlibrary(tikzmark).

The following code

  \begin{tikzpicture}[overlay,remember picture]
    \draw[color=red] (pic cs:#1) circle (0.25cm);

works, but this code

  \begin{tikzpicture}[overlay,remember picture]
    \draw[shorten <=0.2em, #1] (pic cs:#2.west) -- (pic cs:#3.east);

wont draw the line correctly due to the .west and .east. If I remove these, the line is drawn. How can I use .west, .east, .north, and .south properly with tikzmark?

  • Please provide a full example so that we can copy-paste-compile the code. – cfr Mar 28 '15 at 21:27

tikzmark creates coordinates and coordinates do not have distinct anchors because they are points. So coordinate.west would be just the same as coordinate.east and there's no point defining them.

[Drawing on percusse's comment, they are all equivalent to coordinate.center which is just the same as coordinate.]

  • 1
    Coordinates are also node shapes and they only have one anchor which is center. Everything else is mapped to center. – percusse Mar 30 '15 at 0:21

As an extension to cfr's answer, you can define your own \tikzmark

\tikz[overlay,remember picture,baseline]\node [anchor=base] (#1) {#2};
  \begin{tikzpicture}[overlay,remember picture]
    \draw[shorten <=0.2em, #1] (#2.west) -- (#3.east);
  \tikzmark{a}{Some text in tikzmark}

  \tikzmark{b}{Some text in another tikzmark}

Here \node is used instead of \coordinate to make east west etc anchors available. If you are loading tikzmark library, don't forget to give a different name to the macro.

enter image description here

  • (+1) Though it would be easier if we knew how \tikzmark was being used! – cfr Mar 29 '15 at 1:59
  • Thank you! I will use this tikzmark command instead. It would just be easier if I could use the in-built function I think. – Jamgreen Mar 29 '15 at 5:37
  • I've now used your suggestion and it looks fine. But if I use \tikzmark{topA}{-1} in a matrix \begin{pmatrix}...\end{pmatrix} it seems that $-1$ will be interpreted as text instead of a math number. It's obvious that the formatting is different when I have multiple $-1$ values in my matrix. I've also tried \tikzmark{topA} -1 but I guess it's making the cell a 'text cell' if I'm using \tikzmark. – Jamgreen Mar 29 '15 at 10:27
  • @Jamgreen Use \tikzmark{topA}{$-1$} or if you have many of them, change the definition of \tikzmark as \tikz[overlay,remember picture,baseline]\node [anchor=base] (#1) {$#2$}; i.e., enclose #2 inside $. – user11232 Mar 29 '15 at 12:22

cfr's answer is not quite correct, though it is close enough for practical purposes. It is true that tikzmark does not create a node, but neither does it create a coordinate. It doesn't create anything, it does almost the bare minimum needed to mark a point on the page and no more (not quite the bare minimum, there's \pgfmark which is the absolute bare minimum). The point of not making a coordinate or node is that then the position is available before the \tikzmark is issued as well as afterwards (nodes and coordinates would only be available afterwards).

The original \tikzmark was quite like Harish's version, but it was modified (in response to a question here, I think) to the current version to allow for the above situation. Nevertheless, to provide the same functionality as the original, \tikzmark can take an optional first argument which can be any TikZ code.

Thus \tikzmark[{\node[anchor=base] {\(-1\)}}]{a} will produce a node inside the \tikzmark. (Moreover, the name of the tikzmark is passed without "protection" so it can be co-opted to pass further options to the \tikz command as in \tikzmark[stuff]{a,baseline=0pt}.) This isn't quite the same as Harish's version, but to know what to put into the tikzmark arguments I'd need to know exactly what you were trying to do.

However, the philosophy of tikzmark is that it solves the basic problem, but not every problem. If you want to do something complicated then you're better off making a proper tikzpicture or your own macro (as Harish suggests). The number of possibilities for tikzmark are so large that anticipating every single one is more than I can manage. So while you can co-opt the in-built \tikzmark to do what you want, defining a new macro is the "LaTeX way". After all, \tikzmark began life that way.

  • Good ol' times :) – percusse Mar 30 '15 at 0:22

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.