Setting west, east, north, south to position with tikzmark

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

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

  \mbox{}\hfill
  \tikzmark{b}{Some text in another tikzmark}
  \DrawHLine{b}{a}
\end{document}

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


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.


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