I have two problems:
1) I have a bit of longer code that I want to display in my document using the listings
package, wrapped within a figure. It worked well so far, however after compiling the entire document into PDF, the code is cut off at some point and the remaining code is not displayed on any page within the document (see picture below). This was not a problem in the beginning. I also get the warning:
LaTeX Warning: Float too large for page by 970.93338pt on input line 181.
Maybe a solution would be to adjust the textheight somehow? Not sure how to do that in this example. I will provide a MWE below (with a shortened code).
2) I want to frame the code within a box (for which I put the frame = single
command in the preamble) but it does not show. Why is that?
\documentclass[12pt, a4paper]{article}
\usepackage[paper=a4paper,left=3cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage[format=hang,font={small,it},labelfont={bf,it},labelsep=space]{caption}
\captionsetup[figure]{labelsep=space,justification=raggedright,singlelinecheck=off}
\usepackage{listings}
\lstset{
numbers=left,
numberstyle=\small,
numbersep=7pt,
basicstyle=\footnotesize\ttfamily,
breaklines=true,
postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
frame = single}
\lstset{framextopmargin=50pt,frame=bottomline}
\usepackage{courier}
\begin{document}
\subsubsection*{Long-term Analysis: Paris Agreement - Code Example}
\noindent The following code example was used to conduct the \textbf{long-term analysis} of the Paris Agreement for the EURO STOXX 50 index. The analysis was executed using the software R . For the short-term analysis, the R-packages were used.
\bigskip
\\ \noindent Pre: Load necessary packages. Please ensure that R and all packages are up to date, as complications with old versions can sometimes ensue.
\begin{figure}[!htb]
\caption{R-code example: Long-term analysis \label{fig:Codelong}}
\begin{lstlisting}
library(tidyverse) # General data science package
library(readxl) # To import data from excel sheets
library(broom) # To create specific tidy regression output data frames
\end{lstlisting}
\noindent Import firm and market index data sets from an Excel Sheet called "Eurostoxx\_PA\_long.xlsx" which contains the time-series data for each stock and the index from December 2018. (Data in the Excel Sheet has been obtained from Thomson Reuters Eikon.)
\begin{lstlisting}
# Import all firm data into separate data frames (df) named firmname.data using readxl-package
# Example for ABInBev:
abi.data <- subset((read_excel("Eurostoxx_PA_long.xlsx")), select = c("Date", "ABInBev"), col_types = c("date", "numeric"))
# Import market index data into a df called mkt.data
mkt.data <- subset((read_excel("Eurostoxx_PA_long.xlsx")), select = c("Date", "EUROSTOXX50"), col_types = c("date", "numeric"))
# Create lists containing respective low- and high-carbon stocks
low_data <- list(MKT = mkt.data,
Adidas = ads.data,
...)
high_data <- list(MKT = mkt.data,
ABInBev = abi.data,
...)
# Create a function to check NAs for all data frames at once
na <- function(x) {
any(is.na(x))
}
lapply(low_data, na)
lapply(high_data, na)
# Rename columns
new_col_name <- c("Date", "Close")
low_data_clean <- lapply(low_data, setNames, nm = new_col_name)
high_data_clean <- lapply(high_data, setNames, nm = new_col_name)
# Create a function that will calculate the daily returns for all df and adds the outcome to a new column called Return
return <- function(x) { x %>%
mutate(Return = (Close - lag(Close)) / lag(Close))
}
low_data.ret <- lapply(low_data_clean, return)
high_data.ret <- lapply(high_data_clean, return)
# Transfer cleaned/new market data into mkt.data_clean data frame
low_mkt.data_clean <- low_data.ret$MKT
head(low_mkt.data_clean)
# Transfer cleaned/new firm data into new list called firm.data_clean
low_firm.data_clean <- low_data.ret[-1]
# Add mkt.data to each firm data frame in the list as a new column
low_data.mkt <- lapply(low_firm.data_clean, function(x)
mutate(x, MktReturn = (low_mkt.data_clean$Return)))
high_mkt.data_clean <- high_data.ret$MKT
high_firm.data_clean <- high_data.ret[-1]
high_data.mkt <- lapply(high_firm.data_clean, function(x)
mutate(x, MktReturn = (high_mkt.data_clean$Return))
# Delete the first row in all data frames since it does not contain any returns.
low_data.mkt_clean <- lapply(low_data.mkt, function(x) x[-1, ])
high_data.mkt_clean <- lapply(high_data.mkt, function(x) x[-1, ])
# First create a column for the 0/1 dummy variable
low_data.mkt_dum <- lapply(low_data.mkt_clean, function(x)
mutate(x, Dummy = (if_else(x$Date > "2015-12-14", 1, 0))))
# Then create another column where the MktReturn and the Dummy are multiplied
low_data_final <- lapply(low_data.mkt_dum, function(x)
mutate(x, MktRetDum = (x$MktReturn * x$Dummy)))
high_data.mkt_dum <- lapply(high_data.mkt_clean, function(x)
mutate(x, Dummy = (if_else(x$Date > "2015-12-14", 1, 0))))
high_data_final <- lapply(high_data.mkt_dum, function(x)
mutate(x, MktRetDum = (x$MktReturn * x$Dummy)))
# Run regression
low_regression <- lapply(low_data_final, function (x)
lm(x$Return ~ x$MktReturn + x$MktRetDum, data = low_data_final))
# Create tables used later on to display the output
low_df <- lapply(low_regression, function(x)
tidy(x))
high_regression <- lapply(high_data_final, function (x)
lm(x$Return ~ x$MktReturn + x$MktRetDum, data = high_data_final))
# Create tables used later on to display the output
high_df <- lapply(high_regression, function(x)
tidy(x))
# Display regression output in a table that reports the intercept, beta and gamma values for each firm
low_table <- map_df(low_df, ~
.x %>%
mutate(std.error = str_c("(", round(std.error, 4), ")",
c("***", "**", "*", "")[findInterval((p.value/2),
c(0.01, 0.05, 0.1))+1]),
estimate = round(estimate, 4)) %>%
select(estimate, std.error) %>%
t %>%
as.data.frame %>%
rename_all(~ c("Intercept", "Beta", "Gamma")), .id = "Firm")
low_table
high_table <- map_df(high_df, ~
.x %>%
mutate(std.error = str_c("(", round(std.error, 4), ")",
c("***", "**", "*", "")[findInterval((p.value/2),
c(0.01, 0.05, 0.1))+1]),
estimate = round(estimate, 4)) %>%
select(estimate, std.error) %>%
t %>%
as.data.frame %>%
rename_all(~ c("Intercept", "Beta", "Gamma")), .id = "Firm")
high_table
\end{lstlisting}
\end{figure}
\end{document}