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}
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
.
\usepackage{graphicx}
and\textwidth
. I'll add a picture in another comment.Some text \tikz \draw [dashed] (0,0) -- (0.8\textwidth, 0);
You could tweak the0.8
to any number <1, perhaps, that would be obvious.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.