views:

1766

answers:

4

I'm currently doing something like:

a\hfill{}b

which puts 'a' at the far left, and 'b' at the far right of the page. However, I'd like 'b' to be exactly half way. Is there a \hfill equivalent where I can say \hfill{0.5\textwidth}? I don't know the width of 'a'.

Obviously, I could use tabular, or maybe minipage, but the rest of the code here is pretty complicated, so I'd rather not mess with it.

Edit:

a and b are both subfloats (subfigure) with lstinputlistings in them.

+1  A: 

You could try a multicol environment with columnsep set to 0.

There is, by the way, also a way to measure the size of a rendered part, through \settowidth and \settoheight.

Svante
+3  A: 
\documentclass{article}

\begin{document}
\noindent\hbox to 0.5\textwidth{a}b

Curabitur viverra ligula a dui condimentum tempus. Aenean convallis, massa ut fermentum
convallis, leo justo iaculis lectus, quis molestie orci ligula ut leo. Quisque vitae neque   
sit amet tortor convallis rhoncus ac at nibh. Cras sit amet mauris sit amet neque 
tincidunt iaculis non non ante. Proin quis nisi porttitor enim adipiscing bibendum et  
fermentum magna. Morbi sapien nibh, venenatis in fringilla id, laoreet ut libero. 
\end{document}

NB: this works if width of a is less than 0.5\textwidth so the multicol answer is probably more reasonable

RC
I find the text distracting. Why not just show the single line with the solution?
Svante
I use some placeholder text to show the page layout (so you see where \textwidth ends)
RC
On the small example, it looks good. How does this work? Where does the `to` go?
Paul Biggar
\noindent means no indentation then `\hbox to 0.5\textwidth{a}` is read do a horizontal box going from current position to 0.5 * \textwidth containing "a""\hbox to <dimension>" is TeX syntax
RC
This kinda works. The space goes before `a` though in the real document (`a` is a subfloat with an `lstinputlisting` in it). I figured that wasn't relevant, but I guess it is.
Paul Biggar
OK, that didn't work, but I got it with a slight variation:`\noindent\makebox[0.46\textwidth][l]{a}`. It introduced an extra indentation which I didn't know how to prevent, so I just lowered the width of `a`, and changed the box size. Probably unsafe in general, but it works here. Thanks.
Paul Biggar
A: 
a\hfill{}b\hspace{0.5\textwidth}
Guðmundur H
This puts the _right_ border of b to the centre line.
Svante
If b is too wide, you will end with a 30% / 70% ratio with that
RC
A: 

Try replacing \hfill to \hskip 0pt plus 1fill and you can do amazing things. For example,


a\hskip 0pt plus .35 fill b\hskip 0pt plus .65 fill 
Alexey Malistov