1

I am trying to write a macro which will allow me to place work / educational experiences neatly on a page for a CV. To do this, I have designed a table, with three columns, and total width = \textwidth.

The text in the first and second columns should be left-aligned, the text in the last column should be right aligned. Importantly, the text in the middle columns should be aligned for all tables in the document. Currently, the text in the middle columns seems to float and is not aligned across the tables in the document.

Assuming this is due to dynamic column widths, how can I force a specific width (e.g. 30% of \textwidth) to the left-column, so that the middle column will be aligned across all tables?

The macro should be able to be saved separately in a .sty file, and loaded into a .tex document of \documentclass{article}.

My LaTeX macro below:

% Define a macro to build a table (4 args: dates, institution, position, description)
\newcommand{\education}[4]{

% Put the first three args in a table
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}llr@{}}
    \textbf{#1} & \textbf{#2} & \textbf{#3}
\end{tabular*}

% Description with no hanging and in smaller text
\vspace*{5pt}
\noindent \small #4

% Back to normal size
\normalsize \par

% Some space
\vspace*{1em} 
}  
2
  • 2
    Welcome to tex.sx. It's also necessary to know what document class you're using, since that may also define some things affecting what you're trying to do, For best results, provide a small compilable example that shows what you're getting now. It should begin with \documentclass and end with \end{document}. Then a potential helper will have something to experiment with, without guessing. Dec 11, 2022 at 15:32
  • Thanks Barbara.. I added the documentclass now. The issue is more to do with the way that table sizings are calculated in tabular vs tabular*; I think I might require some functionality from both (ability to set the total width alongside the ability to set individual column widths). I think it might also be helpful, whilst designing this, to see the table lines drawn.
    – Zac
    Dec 11, 2022 at 16:51

1 Answer 1

1

The following might be more in line with what you're after:

enter image description here

\documentclass{article}

\usepackage{tabularx}

% Define a macro to build a table (4 args: dates, institution, position, description)
\NewDocumentCommand{\education}{ m m m m }{
  \par
  \addvspace{.7\baselineskip}
  % Put the first three args in a table
  \noindent\begin{tabularx}{\linewidth}{@{} p{.15\linewidth} X wr{.2\linewidth} @{} }
    \bfseries #1% Date 
      & \bfseries #2% Institution
      & \bfseries #3% Position
  \end{tabularx}
  % Description with no hanging and in smaller text
  \par\nobreak\vspace{.5\baselineskip}
  \noindent{\small #4\par}%
  % Some space
  \addvspace{1em} 
}

\begin{document}

\education
  {Date 1}{Institution 1}{Position 1}{Lorem ipsum}

\education
  {Date 2}{Instituion 2 Institution}{Position 2}{Lorem ipsum}

\end{document}

The main suggestion would be to consider using tabularx for spreading paragraph-style columns with dynamic widths across the remainder of your table width via an X-column. So, specify the known/fixed-width content using p{<len>} or wr{<len>} (fixed-width, right-aligned) and insert X for the column you want an automatic width calculation to fill the table.

3
  • Thanks Werner, that looks good. But sorry I should have mentioned that in fact I am trying to store this macro (and others) in a separate .sty file. Will this still work given the references to \linewidth?
    – Zac
    Dec 11, 2022 at 17:32
  • @Zac: Sure; the table with - \linewidth - will be established at the time of the call, which would depend on where it's placed in your document, not whether it's placed in a .sty.
    – Werner
    Dec 11, 2022 at 17:40
  • I am unable to compile this: I get "..../macros.sty:114: LaTeX Error: Missing \begin{document}.". I don't see why it want's a document in the .sty file? (There is a \begin{document} in the .tex file).
    – Zac
    Dec 11, 2022 at 17:45

You must log in to answer this question.

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