4

I am starting in LaTeX. I want to write a document and define some styles.

In this case I want to do two things with one portion of a paragraph:

  1. That it has a red color
  2. That it is aligned to the right

My problem is that I don't know how to do both when defining the environment.

This is my code:

%Define color
\newcommand{\txtred}[1]{\textcolor{red}{#1}}

%Command to use in the context
\newcommand{\atright}[1]{\renewcommand{\atright}{#1}}

\newenvironment{txtbook}
    {\begin{flushleft}}
        {\txtred{\atright}
    \end{flushleft}
}

In the context:

\begin{txtbook}Del libro del Génesis \hfill \atright{9, 8-15}\end{txtbook}

This works, but I want to translate \hfill to the environment, but I don't know how to do it.

I am using memoir, if there is some simpler way to do what I want I would appreciate it.

There is one example, at right, there is the argument:

enter image description here

2
  • Are you able to mock up some examples of what the expected output should look like? Even if you do it in Word (or some other Wysiwyg editor)... consider provide sufficient examples that cover all possible use cases. Can you do that? – Werner Jan 21 at 21:59
  • @Werner I added one image at end. – A. Cedano Jan 21 at 22:15
2

If you really wish to use an environment and your current interface, the following will suffice:

enter image description here

\documentclass{memoir}

\usepackage{xcolor,lipsum}

\newcommand{\txtred}{\textcolor{red}}
\newcommand{\atright}{\mbox{}\hfill\txtred}
\newenvironment{txtbook}{%
  \raggedright
}{%
  \unskip\par
}

\begin{document}

\lipsum[1]

\begin{txtbook}
  Del libro del Génesis \atright{9, 8-15}
\end{txtbook}

\lipsum[2]

\end{document}

One can add some spacing adjustments, if needed.

4
  • Ok, I prefer this approach, because I need to set another things into the environment. Thanks. – A. Cedano Jan 21 at 23:00
  • Do you know who I can add one additional space after the paragraph with txtbook environment? If I add this into de environment: \setlength{\parskip}{1\baselineskip} the space is placed on top of the paragraph, but I need it on bottom. – A. Cedano Jan 22 at 10:19
  • @A.Cedano: Try this code. – Werner Jan 22 at 20:08
  • It's working fine. Thanks a lot! – A. Cedano Jan 22 at 20:40
5

I'd use a command, rather than an environment.

\documentclass{memoir}
\usepackage{xcolor}

\usepackage{lipsum} % for mock text

\newcommand{\txtbook}[2]{%
  \par\addvspace{\topsep}
  {\centering #1\hfill\textcolor{red}{#2}\par}\nopagebreak
  \addvspace{\topsep}
}

\begin{document}

\txtbook{Del libro del Génesis}{9, 8-15}

\lipsum[1]

\end{document}

enter image description here

2
  • It works exactly how I want it. Although, if I try to put the paragraph in an environment it stops working. I suppose I would have to raise that question separately? – A. Cedano Jan 21 at 22:41
  • @A.Cedano I can only guess what you want to achieve… – egreg Jan 21 at 22:45

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.