5

The command \hrulefill prints a line that fills all empty horizontal space with a line. I would like to do something similar with TikZ.

What I would like to know is can I do something similar like

\noindent Some text \hrulefill

with TikZ?

What I would like to do is for TikZ to be able to determine the x-coordinate of the margin so that I can do something like

\tikz Some text \draw [dashed] (0,0) -- (end of textwidth,0);

For more information, you can see my answer in Best way to insert a horizontal cut line with a scissor symbol.

  • I am not sure about what you are asking but if assume things right, you perhaps want \usepackage{graphicx} and \textwidth. I'll add a picture in another comment. – kan Sep 10 '12 at 10:13
  • Somewhat related: tex.stackexchange.com/q/16419/86 – Loop Space Sep 10 '12 at 10:15
  • Image: imgur.com/9Ftq0 Code: Some text \tikz \draw [dashed] (0,0) -- (0.8\textwidth, 0); You could tweak the 0.8 to any number <1, perhaps, that would be obvious. – kan Sep 10 '12 at 10:22
  • @KannappanSampath, that is what I tried to do in the link I gave in my question. I would like TikZ (or TeX) to be able to automatically set the coordinates of the margin so that I would no longer need to set the coordinates by hand every time I change my margin size. – hpesoj626 Sep 10 '12 at 10:25
6

Solution based on package zref-savepos

\documentclass{article}
\usepackage{tikz}
\usepackage{zref-savepos,zref-user}
\providecommand*{\zsaveposx}{\zsavepos}% for older versions
\newcounter{dashlinecnt}
\renewcommand*{\thedashlinecnt}{%
  dashline-\the\value{dashlinecnt}%
}
\newcommand*{\dashedlinefill}{%
  \leavevmode
  \stepcounter{dashlinecnt}%
  \zsaveposx{\thedashlinecnt-a}%
  \zrefused{\thedashlinecnt-a}% 
  % \zrefused{\thedashlinecnt-b}% can be omitted
  \rlap{%
    \tikz\draw[dashed](0,0) -- %
    (\zposx{\thedashlinecnt-b}sp-\zposx{\thedashlinecnt-a}sp,0);%
  }%
  \hfill
  \zsaveposx{\thedashlinecnt-b}%
}

\begin{document}
\noindent
A\hfill B

\noindent
Hello World \dashedlinefill

\end{document}

Result using zref-savepos

Solution based on tikz' remember picture

(Update: overlay added for first \tikz command.)

This is explained in section 16.13 Referencing Nodes Outside the Current Pictures. The example also needs option overlay for the first \tikz command to avoid a shift of the first node and a line with a slight slope.

(Second update:) The reason is that \tikz uses a drawing area with a minimal width and height of 1pt. Thus instead of overlay also

minimum width=0pt,
minimum height=0pt,

can be used for the first \tikz. The second \tikz still requires overlay of course.

\documentclass{article}
\usepackage{tikz}
\newcounter{dashlinecnt}
\renewcommand*{\thedashlinecnt}{%
  dashline-\the\value{dashlinecnt}%
}
\newcommand*{\dashedlinefill}{%
  \leavevmode
  \stepcounter{dashlinecnt}%
  \tikz[remember picture,overlay,inner sep=0pt]\node(\thedashlinecnt){};%
  \hfill
  \tikz[remember picture,overlay,inner sep=0pt]\draw[dashed](\thedashlinecnt) -- (0,0);%
}

\begin{document}
\noindent
A\hfill B

\noindent
Hello World \dashedlinefill

\end{document}

This solution also needs two LaTeX runs. However, in this case a rerun warning is not provided by tikz.

Result based on tikz

  • When I have time I'll go over that part of the pgf manual. – hpesoj626 Sep 11 '12 at 1:28
5

If you want to fill only the current line, then you can use the macro after \tikz ...;

\documentclass{article}
\usepackage{showframe}
\usepackage{tikz}
\pagestyle{empty}
\makeatletter
\def\hdashfill{%
  \leavevmode%
  \cleaders \hb@xt@ 7pt{\hss\rule[1ex]{4pt}{0.5pt}\rule{3pt}{0pt}}\hfill\kern\z@}
\makeatother
\begin{document}

Some text \hdashfill

\tikz\node at(0,0){Some text};\hdashfill

\end{document}
5

Here is an humble attempt:

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\newlength{\sometext}
\settowidth{\sometext}{Some Text }
\noindent Some Text \tikz \draw[dashed](0,0)--({\linewidth-\sometext},0);

%Another variant that Percusse suggested on chat, thanks to Percusse
%This works better in terms of output than the above. I guess the space after 
%text in "Some text  " is not being counted. 

\noindent Some Text \tikz{\pgfmathsetmacro{\mylen}{width("Some Text ")}
\draw[dashed](0,0)-- ({\the\linewidth-\mylen},0);}

\noindent\tikz \draw[dashed](0,0)--({\textwidth-0.4},0);

\end{document}

Output

:-)

Acknowledgements

Thanks to Andrew Stacey who helped with a lot of this code and Percusse who suggested some polishing, which leaves open the question of what my contribution is.

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.