1

I am trying to create a command for right alignment of text. The text must be kept on the same line if it fits on the line, else it must flow to the next line but must keep the alignment (right aligned). Currently I can do these with two separate commands, like:

\renewcommand{\marks}[1]{\ \newline\hfill\textbf{[#1\hphantom{a}marks]}}

or

\renewcommand{\marks}[1]{\ \hfill\textbf{[#1\hphantom{a}marks]}}

However, I am unable to combine them. That is,

  1. if I use the first command then it puts a new line even if there is space on the same line.
  2. if I use the second command then it puts the marks towards the left on the next line if space is unavailable on the same line.

Thanks.

2 Answers 2

2

The following code will solve your problem:

\renewcommand{\marks}[1]{%
  \unskip\nobreak\hfil\penalty50\null\nobreak
  \hfil\textbf{[#1\hphantom{a}marks]}%
  \parfillskip 0pt\finalhyphendemerits0}
2
  • Thanks so much. It worked just fine. Can you please explain what is happening here. Commented Nov 16, 2015 at 11:47
  • These just process your request with penalties and parameters. Commented Nov 16, 2015 at 12:48
1

This implementation allows that any text following the \marks argument gets pushed onto a new line, rather than get tacked onto the same line as the \marks argument.

\documentclass{article}
\renewcommand\marks[1]{\ \hfil\allowbreak\mbox{}\hfil\mbox{\bfseries #1}\break}

\begin{document}
Here is the first text. \marks{Here is the second text.} And here is the next text.

Here is the first text which is also quite longer. \marks{Here is the second text.} And here is the next text.

Here is the first text which is also quite longer.. \marks{Here is the second text.} And here is the next text.

Here is the first text which is more than quite a bit longer. \marks{Here is the second text.} And here is the next text.
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .