TransWikia.com

How to make environments (tabular, multicols, etc.) hug the inner margin and not the left margin?

TeX - LaTeX Asked by SilkyRock on July 16, 2021

I am currently reading LaTeX: A Document Preparation System, 2nd Edition by Leslie Lamport, and for learning purposes am trying to recreate certain structures present in the book.

For instance, in the book the author uses two-column sections to show the code (right column) and the output of the code (left column).
Below is a screenshot of an example:
Two-column section

The problem is that I can’t recreate the same structure (I am using the multicol package for this, with the verbatim environment in the right column) while using the book document class, because on even-numbered pages the two-column section is hugging the left margin.
When the code in the right column is longer than half of textwidth, it gets printed inside the inner margin (example below, lines with black squares).
Bad margins for two-column section

The same happens with tables (made with tabular) which are longer than textwidth (example below).
Bad margins for tables

In all of these cases I would like to make these environments hug the inner margin (right margin for even-numbered pages and left margin for odd-numbered pages) and print the leftover part in the outer margins.
Of course, this should happen only when the width of the environment is bigger than textwidth.

EDIT: Added a minimal working example.

documentclass[12pt,a4paper]{book}

usepackage{blindtext}
usepackage{multicol}

begin{document}

blindtext

begin{multicols}{2}scriptsize
  noindent
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).
  columnbreak
begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{verbatim}
end{multicols}

blindtext

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

blindtext

begin{multicols}{2}scriptsize
  noindent
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).
  columnbreak
begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{verbatim}
end{multicols}

blindtext

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

blindtext

end{document}

One Answer

This uses paracol to reduce the column width rather than deliberately exceeding it elsewhere. You will also note that this approach lines up with the headers the same as the example.

Interestingly, without the verbatim multicols ignored the columnbreak, but a simple paragraph break did the trick.

documentclass[12pt,a4paper]{book}
usepackage[textwidth=6in,showframe]{geometry}
usepackage{blindtext}
usepackage{multicol}
usepackage{paracol}
twosided
setcolumnwidth{0.9textwidth}

begin{document}

begin{paracol}{2}
blindtext
end{paracol}

begin{multicols}{2}scriptsize
  noindent
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).

noindent 
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{multicols}

begin{paracol}{2}
blindtext
end{paracol}

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

begin{paracol}{2}
blindtext
end{paracol}

begin{multicols}{2}scriptsize
  noindent
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).

noindent 
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{multicols}

begin{paracol}{2}
blindtext
end{paracol}

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

begin{paracol}{2}
blindtext
end{paracol}

end{document}

It occurred to me that providing translations is what paracol is primarily used for. I created an new environment (codecols) to handle all the parameter changes needed.

documentclass[12pt,a4paper]{book}
usepackage[textwidth=6.5in,showframe]{geometry}
usepackage{blindtext}
%usepackage{multicol}
usepackage{paracol}
twosided
setcolumnwidth{0.8textwidth}

makeatletter
newenvironment{codecols}{parbigskip
  parindent=0pt
  @minipagetrue% remove spacing arund verbatim
  scriptsize
  setcolumnwidth{dimexpr 0.5textwidth-0.5columnsep}%
  twosided[pmb]% do not swap columns
  begin{paracol}{2}}%
{end{paracol}
  twosided
  setcolumnwidth{0.8textwidth}}
makeatother

begin{document}

begin{paracol}{2}
blindtext
end{paracol}

begin{codecols}
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).
switchcolumn
begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{verbatim}
end{codecols}

begin{paracol}{2}
blindtext
end{paracol}

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

begin{paracol}{2}
blindtext
end{paracol}

begin{codecols}
  An intra-word dash or hyphen, as in X-ray.
  A medium dash (en-dash) for number ranges, like 1--2.
  A punctuation dash---like this (also know as em-dash).
switchcolumn
begin{verbatim}
An intra-word dash or hyphen, as in X-ray.
A medium dash (en-dash) for number ranges, like 1--2.
A punctuation dash---like this (also know as em-dash).
end{verbatim}
end{codecols}

begin{paracol}{2}
blindtext
end{paracol}

begin{center}
  setlength{tabcolsep}{1em}
  begin{tabular}{| l | l | l | l | l | l |}
    hline
    textit{cmd}   & textit{decl}   & textit{cmd}   & textit{decl}    & textit{cmd}   & textit{decl}    
    hline
    verb|textup| & verb|upshape| & verb|textsc| & verb|scshape|  & verb|textrm| & verb|rmfamily| 
    verb|textit| & verb|itshape| & verb|textmd| & verb|mdseries| & verb|textsf| & verb|sffamily| 
    verb|textsl| & verb|slshape| & verb|textbf| & verb|bfseries| & verb|texttt| & verb|ttfamily| 
    hline
  end{tabular}
end{center}

begin{paracol}{2}
blindtext
end{paracol}

end{document}

Answered by John Kormylo on July 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP