1

I'm aware there are similar questions like this on TeX.SE but I'm not competent enough to infer between other examples and mine or to isolate the problematic line of code.

I'm confused because it's not just the size of the text creating the cut off - if I change text size to 12 from 36 it will still be cut off.

Can anybody suggest?

My table and code is below.

Cut off table

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sun, Apr 30, 2017 - 16:53:55

\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & Variable & Party.With.High.Populism.Score & Party.With.Low.Populism.Score \\ 
\hline \\[-1.8ex] 
1 & Nationalism & Supports nationalistic conception of society & Supports cosmopolitan conception of society \\ 
2 & Multiculturalism & Supports assimilation & Supports multiculturalism \\ 
3 & Immigration & Supports a tough stance on immigration & Opposes tough stance on immigration \\ 
4 & Democratic Freedoms & Values tradition and authoritarian values & Values libertarian values \\ 
5 & Civil Liberties & Supports a tough stance on crime & Promotes civil liberties \\ 
6 & Social Lifestyle & Opposes liberal attitudes to lifestyle & Favours liberal attitudes to lifestyle \\ 
7 & Establishment & Critical of the establishment & Supportive of the establishment \\ 
8 & Religion & Supports religions inflluence on politics as 'moral authority' & Opposes religions influence on politics \\ 
9 & Urban/Rural & Supports rural rather than urban  interests & Supports urban rather than rural interests \\ 
10 & Environment & Prioritises economic growth over environment protection & Prioritises environmental protection over economic growth \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 
CarLaTeX
  • 62,716
  • 1
    Please make your example compilable. – TeXnician Apr 30 '17 at 15:10
  • @TeXnician, sorry - not sure what that means! – Henry Cann Apr 30 '17 at 15:15
  • 1
    I mean that this can't be the complete document as the relevant part (documentclass, packages and document environment are missing). – TeXnician Apr 30 '17 at 15:24
  • Shortly after you posted I deleted the bit at the top saying "table created by..." thinking it was irrelevant - that what you mean? Added this back in. Does it explain anything that I created this output in R? Otherwise, I'm confused because this is all there is. – Henry Cann Apr 30 '17 at 15:29
  • 3
    the table is wider than the page but (for example) you haven't said how wide the page is. The width of the table depends on the font but (for example) you haven't said what fonts are being used. that's why it's always best to make complete examples from \documentclass to \end{document} that said cccc columns do not allow line breaking change some of the c to say p{3cm} to have 3cm wide columns, or whatever fits. – David Carlisle Apr 30 '17 at 15:32
  • @DavidCarlisle I've tried adding in \documentclass in the preamble but even though I've put it before the begin table line, it says can only be used in preamble. I've noticed that my example contains \begin{table}[!htbp] \centering but does not have \begin{document} - could that be a problem? – Henry Cann Apr 30 '17 at 16:07
  • 2
    @HenryCann yes of course, every latex document has to have \begin{document}. How did you make the image you show? you presumably included the fragment of code with the table into a latex document? – David Carlisle Apr 30 '17 at 16:21
  • @DavidCarlisle Using R Studio, the command stargazer(gg2, type = "latex", out = "heh2.tex", summary = FALSE) where gg2 is the data frame object read into R from excel, produces output which I then paste directly into LaTeXIT from the R Studio console output. It also creates a file called "heh2.tex". I normally use this method for regression output but have successfully created other basic tables in the last week. Bizarre that it works given that you say the begin document command is essential? – Henry Cann Apr 30 '17 at 16:31
  • 1
    @HenryCann presumably one of the tex files that gets made has \docuemntclass and \begin{document} \end{document} (otherwise you would get no output from latex) – David Carlisle Apr 30 '17 at 17:04

1 Answers1

2

Do yourself a big favor and learn how to use the tabularx environment. Doing so will free you from artificial (and likely unsatisfactory) limitations imposed by software that purports to "automate" the generation of tabular material. You can still use such a tool to create an initial mock-up of the table. Just don't treat the mock-up as the final, or near-final state of the production process.

In the following table, the contents are unchanged from what you posted. What has changed significantly is the header setup. E.g., the material in the columns is now left-aligned (with automatic line-breaking, to make sure the table isn't too wide) rather than centered.

enter image description here

\documentclass{article}
\usepackage[english]{babel}
\usepackage{booktabs,tabularx,caption,ragged2e}
\captionsetup{skip=0.333\baselineskip}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newlength\mylength
\settowidth\mylength{Multiculturalism} % width of 2nd col.
\begin{document}
\begin{table}[!htbp]
\setlength\tabcolsep{4pt} % default: 6pt
\caption{}  \label{} 
\begin{tabularx}{\textwidth}{@{} l p{\mylength} LL @{}}  
\toprule 
 & Variable & Party with High Populism Score & Party with Low Populism Score \\ 
\midrule
1 & Nationalism & Supports nationalistic conception of society & Supports cosmopolitan conception of society \\ 
2 & Multiculturalism & Supports assimilation & Supports multiculturalism \\ 
3 & Immigration & Supports a tough stance on immigration & Opposes tough stance on immigration \\ 
4 & Democratic Freedoms & Values tradition and authoritarian values & Values libertarian values \\ 
5 & Civil Liberties & Supports a tough stance on crime & Promotes civil liberties \\ 
6 & Social Lifestyle & Opposes liberal attitudes to lifestyle & Favours liberal attitudes to lifestyle \\ 
7 & Establishment & Critical of the establishment & Supportive of the establishment \\ 
8 & Religion & Supports religion's influence on politics as `moral authority' & Opposes religion's influence on politics \\ 
9 & Urban/Rural & Supports rural rather than urban  interests & Supports urban rather than rural interests \\ 
10& Environment & Prioritises economic growth over environment protection & Prioritises environmental protection over economic growth \\ 
\bottomrule
\end{tabularx} 
\end{table}
\end{document}
Mico
  • 506,678
  • thank you so much for this it's perfect. I just need to ask one question. Normally do create tables I paste my output directly into Latex, click on the 'Text' tab, and go 'Latex it' to make the table. For some reason when I paste this in I get some error messages starting with 'can only be used in preamble'. Am I doing something wrong in the table creation? – Henry Cann May 01 '17 at 09:42
  • Or to put it differently, what program did you use to create the example above @Mico? Many thanks – Henry Cann May 01 '17 at 10:05
  • 1
    @HenryCann what mico posted is the complete input to latex, as discussed in comments under the question you are just posting the fragment with the table. If you use a complete document as here and use software that wraps it in an additional \documentclass{article}\begin{document}... then you would get exactly the error that you state. – David Carlisle May 01 '17 at 10:35
  • @HenryCann - Please reveal which front-end program you use to edit your documents. – Mico May 01 '17 at 10:41
  • @HenryCann - I used LaTeX to create the table; the entire LaTeX document is shown above in my answer. By the way, statements such as "I paste my output directly into Latex" and "when I paste this in" are rather vague and not actionable: We don't know (a) what exactly is being pasted and (b) *which program" it's being pasted into. Do provide a bit more information about how you create your documents. – Mico May 01 '17 at 11:01
  • Sorry for coming back late to this. I use R Studio using packages such as Stargazer and x table. I create objects and pass them to the table / stargazer function in R which produces the code fragment you saw above. I didn't realise until posting here that the code produced is not complete. @Mico - I will endeavour to learn the environment as you suggest - could you tell me though is there any one specific line or character controlling the number of columns? Otherwise, what is the part of your code specifically controlling width and making it fit the data? Thanks – Henry Cann May 09 '17 at 13:37
  • @HenryCann - The crucial line of code is \begin{tabularx}{\textwidth}{@{} l p{\mylength} LL @{}}. It states that the tabularx environment has width \textwidth and that it contains four columns: the first is of type l ("left-aligned", no line-breaking); the next is of type p, with width equal to the word "Multiculturalism", with automatic line breaking as needed; and the final two columns are of type L, where L is defined in the preamble as a modified form of the X column type that's provided by the tabularx package. Please read any serious LaTeX intro for more details. – Mico May 09 '17 at 14:07
  • Thanks @Mico - Naive question but would turning LL @{}} into LLLL @{}} be all I needed to do to add two more columns? – Henry Cann May 09 '17 at 14:16
  • @HenryCann - Sure, writing LLLL instead of LL would set up 4 rather than just 2 columns of type L. You realize that each of the four new columns would only be half as wide as the former two columns, right? Do please take the time to study the user guide of the tabularx passage to inform yourself about the tabularx environment and the column type X. (The L column type is just a modified version of the X column type.) – Mico May 09 '17 at 14:22
  • Er, No I didn't, but I do now that I've tried it and the content in most of the columns are wildly overlapping!!! Is there a default way to make all columns snap to the size of the text, I.e to fit? Ok will try and take a look at the documentation. It's something I'd like to become comfortable with it but the way I'm trying to learn is hardly ideal, i.e on the fly for a paper. – Henry Cann May 09 '17 at 14:26
  • @Mico Just for update I made progress using \newcolumntype{L}{>{\hsize=7.5\hsize}X} to control the width of the L columns. This has worked pretty well, may only problem is making all my columns fit onto the page, since the example I am trying this with has a full 7 columns. Thanks for all the help. – Henry Cann May 09 '17 at 14:41
  • @HenryCann - I must confess to not understanding what you're doing. All columns of (basic) type X usually have the same width; the \hsize device can be used to set up X-type columns of different widths. The cardinal rule is that the sum of the numbers in front of the various X columns has to be equal to the number of columns of type X. E.g., >{\hsize=0.667\hsize}L>{\hsize=1.333\hsize}L sets up 2 columns of type L, with the first being roughly twice as wide as the second. Note that 1.333+0.667=2, which equals the number of L columns. – Mico May 09 '17 at 15:14
  • @HenryCann - Feel free to post a new query to present the issues you're experiencing with the new table. – Mico May 09 '17 at 15:15