3

I tend to use \paragraph{text} to structure my text (instead of \subsubection for example). The text in the paragraph is printed bold and some extra spacing is inserted. I would like to alter this by having latex print a period after the bold text. I could achieve this effect by defining another macro like.

\newcommand{\myparagraph}[1]{\paragraph{#1.}}

Is there some way to achieve the same effect using a more latex-friendly way (like a \paragraphDelimiter variable)?

  • You simply want to add a period to the paragraph title automatically? – Alenanno Sep 7 '16 at 9:40
  • 1
    your code fragments are so small it is impossible to guess what you are doing. \paragraph is a fourth level section heading following section,subsection,subsubsection. Despite its name it doesn't have much to do with paragraphs of text. You could modify the definition copied from article class (which is only a line) or use any package that helps set up section formatting. – David Carlisle Sep 7 '16 at 9:44
  • 1
    If you load titlesec, then you can do \titleformat{\paragraph}[runin]{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}[.]. So if you write \paragraph{A title}, this will yield A title. in the resulting PDF. – Alenanno Sep 7 '16 at 9:49
8

Copy the definition of \paragraph from your class (here I used article) and modify it by just adding \maybe@addperiod as shown: it should be the last token in the last argument.

\documentclass{article}
\usepackage{amsthm} % for \@addpunct

\makeatletter
\renewcommand\paragraph{%
  \@startsection{paragraph}
    {4}
    {\z@}
    {3.25ex \@plus1ex \@minus.2ex}
    {-1em}
    {\normalfont\normalsize\bfseries\maybe@addperiod}%
}
\newcommand{\maybe@addperiod}[1]{%
  #1\@addpunct{.}%
}
\makeatother

\begin{document}

\paragraph{This is the title} And some text follows.

\paragraph{This is the title.} And some text follows.

\paragraph{Is this is a title?} And some text follows.

\end{document}

As you can see, the period is printed only if punctuation doesn't end the argument to \paragraph.

If you want a version that doesn't need looking for the definition:

\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm} % for \@addpunct

\makeatletter
\let\latexparagraph\paragraph
\RenewDocumentCommand{\paragraph}{som}{%
  \IfBooleanTF{#1}
    {\latexparagraph*{#3}}
    {\IfNoValueTF{#2}
       {\latexparagraph{\maybe@addperiod{#3}}}
       {\latexparagraph[#2]{\maybe@addperiod{#3}}}%
  }%
}
\newcommand{\maybe@addperiod}[1]{%
  #1\@addpunct{.}%
}
\makeatother

\begin{document}

\paragraph{This is the title} And some text follows.

\paragraph{This is the title.} And some text follows.

\paragraph{Is this is a title?} And some text follows.

\end{document}

enter image description here

  • I think this may be a little over-engineered, but it works. I do also like Alenannos suggestion. – hfhc2 Sep 7 '16 at 10:48
  • 1
    @hfhc2 If you happen to have a title ending with punctuation different from a period, the simpler solution won't work. – egreg Sep 7 '16 at 11:13
  • Also check out the magisterial handling of titles \paragraph{Like ``this.'' } – Joe Corneli Sep 11 '16 at 21:19
  • @JoeCorneli Which should work, I guess. ;-) – egreg Sep 11 '16 at 21:22
1

egreg's and Alenanno's solutions are great. For a simpler one, how about the following?

\documentclass{article}
\let\oldparagraph=\paragraph
\renewcommand\paragraph[1]{\oldparagraph{#1.}}
\begin{document}

\paragraph{Hello}

World.

\end{document}

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.