4

I have to write an article with many blocks embedding equations and the output from R. I've decided to use the package minted for everything as the best option to avoid complex formating.

I've also defined general spacing and font sizes.

\RequirePackage{luatex85}
\documentclass[11pt, a4paper, spanish]{article}
\usepackage[left=3.5cm, right=3.4cm, top=4.7cm, bottom=4.1cm]{geometry} 
\renewcommand{\baselinestretch}{1.54}
\usepackage{parskip}   % separación entre parrafos
\parskip=12pt
\setlength{\parindent}{2em} 
\usepackage{polyglossia}    
\setmainlanguage{spanish}
\usepackage[no-math]{fontspec}
\setmainfont{TeXGyrePagellaX}
\usepackage{float}
\usepackage{minted}
\usepackage{indentfirst}
\usepackage[usenames,dvipsnames]{xcolor}
\definecolor{bg}{rgb}{0.92,0.97,1}
\begin{document}

\section{First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\begin{minted}[fontsize=\small, bgcolor=bg]{text}
    E=m·c^2
    some code
\end{minted}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
\begin{minted}[fontsize=\small, bgcolor=bg, escapeinside=||, mathescape=true]{text}
    |$E=m·c^2$|
\end{minted}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\end{document}

The result looks like this:

enter image description here

It seems OK, but how can I change the line spacing before and after the minted text? I would like to have two kind of minted text on my article, one like it is now, independent paragraphs, but another kind with simple spacingtimgur.

How can I get in my example simple line spacing, as if it was all the same paragraph?

Something like this: enter image description here

I need to write many equations and small minted code but I don't want to waste much space.

I prefer a simple solution, sort of command to remove previous space instead of large macro definitions.

I would also like to know how to keep the previous text and the minted block together on the same page if there is a pagebreak. Otherwise latex mixes large tables and figures and is difficult to keep track of things.

PS: I have created a new related question: How to reduce the space before and after minted text with captions (listing)? where I'm asking how to reduce the margins when using captions, within a listing environment.

  • You set \parskip to 12pt, \parindent to 2em and \baselinestretch to 1.54? Isn't it too much? I can understand you need “doublespacing”, but then why adding a nonzero parskip? – egreg Jul 26 '19 at 22:36
4

You can remove the \parskip after a minted environment, with the help of etoolbox.

\documentclass[11pt, a4paper, spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[left=3.5cm, right=3.4cm, top=4.7cm, bottom=4.1cm]{geometry}

\usepackage[skip=12pt,indent=2em]{parskip}   % separación entre parrafos
%\usepackage{indentfirst} % babel-spanish does it
\usepackage{setspace}
\usepackage{textcomp}
\usepackage{etoolbox}

\usepackage{minted}
\usepackage[usenames,dvipsnames]{xcolor}

\definecolor{bg}{rgb}{0.92,0.97,1}
\setstretch{1.54}

\DeclareUnicodeCharacter{00B7}{\TextOrMath{\textperiodcentered}{\cdot}}
\AtBeginEnvironment{minted}{\setlength{\parskip}{0pt}}

\begin{document}

\section{First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\begin{minted}[fontsize=\small, bgcolor=bg]{text}
    E=m·c^2
    some code
\end{minted}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
\begin{minted}[fontsize=\small, bgcolor=bg, escapeinside=||, mathescape=true]{text}
    |$E=m·c^2$|
\end{minted}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\end{document}

I have added some improvements to your code.

enter image description here

However, a large \parskip along with double spacing and indentation is really too much. If you're forced to use double spacing, at least don't set the parskip.

With your setting with polyglossia, here's the code.

\documentclass[11pt,a4paper]{article}
\usepackage[left=3.5cm, right=3.4cm, top=4.7cm, bottom=4.1cm]{geometry}
\usepackage{unicode-math}
\usepackage{polyglossia}

\usepackage[skip=12pt,indent=2em]{parskip}   % separación entre parrafos
%\usepackage{indentfirst} % polyglossia does it
\usepackage{setspace}
\usepackage{textcomp}
\usepackage{etoolbox}

\usepackage{minted}
\usepackage[usenames,dvipsnames]{xcolor}

\setmainlanguage{spanish}
\setmainfont{TeXGyrePagellaX}
\setmathfont{TeX Gyre Pagella Math}

\definecolor{bg}{rgb}{0.92,0.97,1}
\setstretch{1.54}

%\DeclareUnicodeCharacter{00B7}{\TextOrMath{\textperiodcentered}{\cdot}}
\AtBeginEnvironment{minted}{\setlength{\parskip}{0pt}}

\begin{document}

\section{First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\begin{minted}[fontsize=\small, bgcolor=bg]{text}
    E=m·c^2
    some code
\end{minted}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
\begin{minted}[fontsize=\small, bgcolor=bg, escapeinside=||, mathescape=true]{text}
    |$E=m·c^2$|
\end{minted}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis 
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\end{document}
| improve this answer | |
  • I'm not using babel because I use polyglossia instead, though I haven't included all my preamble. – skan Jul 27 '19 at 22:16
  • Why doesn't it work by just using "~" before? – skan Jul 27 '19 at 22:25
  • 1
    @skan I added the changes for the setting. – egreg Jul 27 '19 at 22:40
  • 1
    @skan You don't need the line with XeLateX or LuaLaTeX. – egreg Jul 28 '19 at 8:57
  • 1
    @skan That's a completely different matter. – egreg Sep 10 '19 at 18:59

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.